物体の固定2
物体の固定は、「物体の固定」で紹介した方法の他にFixedJointを利用する方法があります。
FixedJointは、BodyとBodyを見えないジョイントで繋げることができます。
FixedJointは、BodyとBodyを見えないジョイントで繋げることができます。
物体をある位置に固定する場合は、NULLとBodyを接続することで実現できます。
以下の記述がそれにあたります。
以下の記述がそれにあたります。
// 物体の固定
dJointID fixed;
fixed = dJointCreateFixed( world, 0 );
dJointAttach( fixed, NULL, body_box ); // 固定したいボディをNULLと接続
dJointSetFixed( fixed ); // Fixedジョイント設定
重力や質量を設定しても、Boxは空中に浮いた状態のままです。
ソースコード
#ifdef WIN32
#include <windows.h>
#endif
#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
static dWorldID world;
static dBodyID body_box;
dReal box_sizes[3] = { 1.0, 1.0, 1.0 };
#ifdef dDOUBLE
#define dsDrawBox dsDrawBoxD
#endif
// start simulation - set viewpoint
static void start()
{
static float xyz[3] = { 0.f, 5.f, 3.f };
static float hpr[3] = { -90.f, -15.f, 0.f };
dsSetViewpoint( xyz, hpr );
}
// simulation loop
static void simLoop( int pause )
{
// Ctl+p が押されたらifに入らない
if (!pause)
{
dWorldStep( world, 0.01 );
}
dsSetColor( 1.0f, 1.0f, 0.0f );
dsDrawBox( dBodyGetPosition( body_box ), dBodyGetRotation( body_box ), box_sizes );
}
int main( int argc, char* argv[] )
{
dInitODE();
// setup pointers to drawstuff callback functions
dsFunctions fn;
fn.version = DS_VERSION;
fn.start = &start;
fn.step = &simLoop;
fn.command = 0;
fn.stop = 0;
fn.path_to_textures = "textures";
world = dWorldCreate();
dWorldSetGravity( world, 0.0, 0.0, -9.8 );
// body_box creating
body_box = dBodyCreate( world );
dReal pos[3] = { 0.0, 0.0, 3.0 };
dBodySetPosition( body_box, pos[0], pos[1], pos[2] );
// 物体の固定
dJointID fixed;
fixed = dJointCreateFixed( world, 0 );
dJointAttach( fixed, NULL, body_box );
dJointSetFixed( fixed );
dsSimulationLoop( argc, argv, 320, 240, &fn );
dWorldDestroy( world );
dCloseODE();
return 0;
}
以上
添付ファイル