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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM