If you can't explain it simply,

you don't understand it well enough.

ODE仿真引擎使用(三)

  这展示了一个超级简单的示例程序,它使用了ODE (Open Dynamics Engine)。在许多编程书籍中,打印“Hellow World”是第一个示例程序。对于物理仿真模拟编程来说,我认为自由落体模拟是最简单的例子。

  主要包含知识点:创建world,创建body。

创建世界和地面:

1 world = dWorldCreate();
2 space = dHashSpaceCreate(0);
3 contactgroup = dJointGroupCreate(0);
4 dWorldSetGravity(world, 0, 0, -9.8); 
5 ground = dCreatePlane(space, 0, 0, 1, 0);

创建实物:

 1 dMass m1;
 2 dReal x0 = 0.0, y0 = 0.0, z0 = 2.5;
 3 ball.radius = 0.2;
 4 ball.mass = 1.0;
 5 ball.body = dBodyCreate(world);
 6 dMassSetZero(&m1);
 7 dMassSetSphereTotal(&m1, ball.mass, ball.radius);
 8 dBodySetMass(ball.body, &m1);
 9 dBodySetPosition(ball.body, x0, y0, z0);
10 ball.geom = dCreateSphere(space, ball.radius);
11 dGeomSetBody(ball.geom, ball.body);

详细程序:

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

#ifdef dDOUBLE
#define dsDrawSphere dsDrawSphereD
#endif

static dWorldID world;
static dBodyID ball;

const dReal   radius = 0.2;
const dReal   mass = 1.0;

static void simLoop(int pause)
{
    const dReal* pos, * R;
    dWorldStep(world, 0.05);
    dsSetColor(1.0, 0.0, 0.0);
    pos = dBodyGetPosition(ball);
    R = dBodyGetRotation(ball);
    dsDrawSphere(pos, R, radius);
}

void start()
{
    static float xyz[3] = { 0.0,-3.0,1.0 };
    static float hpr[3] = { 90.0,0.0,0.0 };
    dsSetViewpoint(xyz, hpr);
}

int main(int argc, char** argv)
{
    dReal x0 = 0.0, y0 = 0.0, z0 = 1.0;
    dMass m1;

    // setup pointers to drawstuff callback functions
    dsFunctions fn;
    fn.version = DS_VERSION;
    fn.start = &start;
    fn.step = &simLoop;
    fn.command = NULL;
    fn.stop = 0;
    fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;

    // create world 
    dInitODE2(0);
    world = dWorldCreate();
    dWorldSetGravity(world, 0, 0, 0.0001);

    // create ball
    ball = dBodyCreate(world);
    dMassSetZero(&m1);
    dMassSetSphereTotal(&m1, mass, radius);
    dBodySetMass(ball, &m1);
    dBodySetPosition(ball, x0, y0, z0);

    // run simulation
    dsSimulationLoop(argc, argv, 352, 288, &fn);

    dWorldDestroy(world);
    dCloseODE();
}

 

posted @ 2020-02-06 10:50  赵小亮  阅读(866)  评论(0编辑  收藏  举报


Though the night was made for loving,
and the day returns too soon,
yet we'll go no more a-roving
by the light of the moon.