ことはじめ

簡単!実践!ロボットシミュレーション - Open Dynamics Engineによるロボットプログラミング
を読書中。
1個目のプログラムが存外に手間取った(環境整備的な意味で)。

自由落下。

#include <ode/ode.h>
#include <drawstuff/drawstuff.h>

dWorldID g_world;
#define STEP 0.01

dBodyID g_apple;
dReal g_apple_radius=0.2; // 20cm
dReal g_apple_mass=1.0; // 1.0kg

// view
float g_xyz[]={3.0, 0.0, 1.0};
float g_hpr[]={-180.0, 0.0, 0.0};

void drawSphere(const double *pos, const double *rot, double radius)
{
  printf("pos: %f, %f, %f\n", pos[0], pos[1], pos[2]);
  dsDrawSphereD(pos, rot, radius);
}

void simLoop(int pause)
{
  dWorldStep(g_world, STEP);
  drawSphere(
      dBodyGetPosition(g_apple),
      dBodyGetRotation(g_apple),
      g_apple_radius);
}

void start()
{
  dsSetViewpoint(g_xyz, g_hpr);
}

int main(int argc, char **argv)
{ 
  dInitODE(); // 古いサンプルにはついてないので注意!!

  g_world=dWorldCreate();
  dWorldSetGravity(g_world, 0, 0, -0.2);

  g_apple=dBodyCreate(g_world);
  dMass mass;
  dMassSetZero(&mass);
  dMassSetSphereTotal(&mass, g_apple_mass, g_apple_radius);
  dBodySetMass(g_apple, &mass);
  dBodySetPosition(g_apple, 1.0, 1.0, 2.0);

  dsFunctions fn={
    DS_VERSION, // int version;			/* put DS_VERSION here */
    &start, // void (*start)();		/* called before sim loop starts */
    &simLoop, // void (*step) (int pause);	/* called before every frame */
    NULL, // void (*command) (int cmd);	/* called if a command key is pressed */
    NULL, // void (*stop)();		/* called after sim loop exits */
    "../drawstuff/textures", // const char *path_to_textures;	/* if nonzero, path to texture files */
  };
  dsSimulationLoop(argc, argv, 640, 480, &fn);
  dWorldDestroy(g_world);

  dCloseODE(); // 古いサンプルにはついてないので注意!!
  return 0;
}

ビルドに関しては、drawstuffの扱いが若干面倒。
たぶん、drawstuffはデモ用のお手軽ライブラリという位置づけでこれでちゃんとしたアプリを作るようなものではない。そのためodeをインストールしてもシステムにはインストールされないので、必要部分をコピーしてくるかINCLUDE_PATH等を通すなど一手間必要になる。

# drawstuffを../drawstufにコピーしてきている
# ../drawstuff
#     +drawsutff
#        +drawstuff.h
#        +version.h
#     +debug
#        +libdrawstuff.a
#     +textures
#        +*.ppm         
$ g++ main.cpp `pkg-config --cflags --libs ode` -I../drawstuff -L../drawstuff/debug -ldrawstuff -lGL -lGLU

その他メモ

ODEは、「右手系」
drawstuffは「右手系Z-up」
drawstuffのカメラは初期状態で+x方向を向いている。
drawstuffはシーンの状況に関係なく空と地面を描画する。
地面のパラメータは[0, 0, 1, 0](上向きに原点を通る)。


良く見ると地面の9つの点がXY座標系になっていて、赤がXに+1、青がYに+1なのであった。