ODE Tutorial 4: Install (Linux) ODE Tutorial 6: Collision Detection
11 月 28

This time,  I show a super simple sample program which uses ODE (Open Dynamics Engine) .  In many programming books, printing "Hellow World" is the first sample program.  For physics simulation programming books, I think a simulation of free fall is the most simple  example.

ODE

A screen shot of  free fall

CODE:
  1. //  hello.cpp by  Kosei Demura
  2.  
  3. #include  //ODE the header file
  4. #include  //drawing header file
  5.  
  6. #ifdef  dDOUBLE
  7. #define dsDrawSphere  dsDrawSphereD
  8. #endif
  9.  
  10. static dWorldID world;   // For kinetics calculation
  11. dBodyID ball;   // Ball
  12. const dReal radius = 0.2, mass = 1.0// Radius of ball [m], weight of ball [kg]
  13.  
  14. // Simulation loop is called and executed each simlation loop.
  15. // dynamics is calculated by dWorldStep().
  16. static void simLoop (int pause)
  17. {
  18. const dReal *pos, *R;
  19.  
  20. // If you want more accuracy, you should set step size more small
  21. // in this case, step size is 0.01 [s]
  22. dWorldStep (world,0.01);
  23.  
  24. dsSetColor (1.0,0.0,0.0);         // Setting of red  (R, G, B)
  25. pos = dBodyGetPosition (ball)// Getting the position of the ball
  26. R = dBodyGetRotation (ball);    // Getting the orientation of the ball
  27. dsDrawSphere (pos, R, radius); // Drawing the sphere as the ball
  28. }
  29.  
  30. void START ()
  31. { //setting of camera
  32. static float xyz [ 3 ] = {0.0, -3.0,1.0}// Position of a view point [m]
  33. static float hpr [ 3 ] = {90.0,0.0,0.0};   // Direction of a gaze [°]
  34. dsSetViewpoint (xyz, hpr)// Setting of the view point and the gaze
  35. }
  36.  
  37. // Please read from main functional here.
  38. int main (int argc,  char **argv)
  39. {
  40. dReal x0 = 0.0, y0 = 0.0, dReal z0 = 1.0// Initial position of a ball
  41. dMass m1;
  42.  
  43. dsFunctions fn;                // Class for draw staff
  44. fn.version = DS_VERSION;  // Version of draw staff
  45. fn.start = &start;             // Preprocessing function of simulation
  46. fn.step = &simLoop;  // A function is called with each step of simulation
  47. fn.command = NULL; //  A function for a key board
  48. // In the case of no function, appointing the NULL pointer
  49. fn.stop = NULL;      //
  50. fn.path_to_textures = "../.. /drawstuff/textures"// Path to the texture files
  51.  
  52. // Creation of the world for dynamics calculation
  53. world = dWorldCreate ();
  54. dWorldSetGravity (world,0,0, -0.001); // Acceleration of free fall [m/s^2]
  55.  
  56. // Creation of a ball
  57. body = dBodyCreate (world)// Creation of  a ball
  58. dMassSetZero (&m1)// Initialization of a weight parameter m1
  59. dMassSetSphereTotal (&m1, mass, radius); // Calculating m1 of the ball
  60. dBodySetMass (ball, &m1)// Setting m1 to the ball
  61. dBodySetPosition (ball, x0, y0, z0)// Setting the position of the ball
  62.  
  63. // Simulation loop
  64. dsSimulationLoop (argc, argv,352,288, &fn)// Window Size 352 x 288
  65.  
  66. // Destruction of the world
  67. dWorldDestroy (world);
  68.  
  69. return 0;
  70. }

This is the program of free fall of the red ball.  "world" is the software container where kinetics is calculated. Being for the first time to experient the world of physics, let's call this sample program as "Hello World".

The function which starts with the small letter 'd'  is the API (Application Program Interface), and the function which starts with 'ds' is the API of  drawstuff. Drawstuff is the library for ODE, to test sample programs.

Now, let's downloadthis file, and excute the program as follows !

Execution method
1. Sample1.tgz download under /home/ user name /src/ode/ode
2. From now on please execute the command below with the terminal. The $ is prompt.
cd /home/ user name /src/ode/ode
tar xvzf sample1.tgz
cd  sample1
make
3. Execution
$. /sample1

The red ball is falling slowly, but passing through the ground, and vanished.
To tell the truth it meaning that collision detection function is not installed in the program above.

The next time, collision detection function is added in this program.

(last modified 2008-1-13)

投稿:06年11月28日

6 Responses to “ODE Tutorial 5: Hello a Simulation World !”

  1. demu MonsterID Icon demu Says at 2008-08-26 :

    Hello Anonymous,

    This article is for ODE 0.9. From ODE 0.10, build system is changed, so you cannot compile using the makefile.

    I wrote some articles how to compile with ODE 0.10 in Japanese. Please see or translate by a machine.
    http://demura.net/wordpress/tutorials/ode2

    I know I must change this article, but it takes some time.

    Cheers,

    demu

  2.  MonsterID Icon 匿名 Says at 2008-08-25 :

    hello
    i dont know whats wrong with the make file but i get

    c:\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -ldrawstuff

    im using last version of ode..

  3. demu MonsterID Icon demu Says at 2007-12-05 :

    Hi Chengsen !

    Time step 0.1 is too big.
    Please set more small value, it improves accuracy.

    demu

  4. Chengsen MonsterID Icon Chengsen Says at 2007-12-05 :

    Dear demu,
    May I ask a question on ODE here?
    I want to simulate a sphere falling down from position (0,0,0), without initial velocity. The sphere is on earth (g = 9.8m/s/s), and the simulation step is 0.1s.

    (1) Here is my program.

    #include

    #include

    #define time_step (float)0.1

    int main()

    {

    dWorldID myWorld_id;

    dBodyID mySphere_id;

    dMass sphereMass;

    const dReal *pos;

    float time = 0.0;

    myWorld_id = dWorldCreate();

    mySphere_id = dBodyCreate( myWorld_id );

    dWorldSetGravity( myWorld_id, 0, 0, -9.8 );

    dBodySetPosition( mySphere_id, 0, 0, 0 );

    dMassSetSphere( &sphereMass, 1, 2 );

    dBodySetMass( mySphere_id, &sphereMass );

    dBodySetLinearVel( mySphere_id, 0.0, 0.0, 0.0 );

    while (time < 1.0) {

    static int i = 0;

    dWorldStep( myWorld_id, time_step );

    pos = dBodyGetPosition( mySphere_id );

    std::cout <<i++ << " :position (" << pos[0] << ", "

    << pos[1] << ", " << pos[2] << ")\n";

    time += time_step;

    }

    dBodyDestroy( mySphere_id );

    dWorldDestroy( myWorld_id );

    return 0;

    }

    (2) The output is,

    0 :position (0, 0, -0.098)

    1 :position (0, 0, -0.294)

    2 :position (0, 0, -0.588)

    3 :position (0, 0, -0.98)

    4 :position (0, 0, -1.47)

    5 :position (0, 0, -2.058)

    6 :position (0, 0, -2.744)

    7 :position (0, 0, -3.528)

    8 :position (0, 0, -4.41)

    9 :position (0, 0, -5.39)

    (3) My question is, why the z position of the sphere is not equal to h=1/2*g*t*t, which will be -0.049, -0.196,……?

    Would you please kindly tell me the meaning of the output?

    Thank you for your help!
    Chengsen

  5. demu MonsterID Icon demu Says at 2007-10-21 :

    Thank you very much for buying my book.

    Now, I stay in Seoul to demonstrate my RoboCup humanoid soccer robot.
    I will demonstrate in Indiana Hall, COEX, Seoul at 13:30 and 14:30 in 21 Octorber, 2007.

    I changed my web site that you can download my program.

    demu

  6. Kime jeong soo MonsterID Icon Kime jeong soo Says at 2007-10-19 :

    Hello

    My name is jeong soo kim in korea.

    I want to learn ODE.

    I bought your book yesterday.

    I can't download both sample1.tgz and sample2.tgz .

    I hope that you send those file to me.

    Thank you.

Leave a Reply

カウンタ (since 2008-1-11)
Copyright © 1998-2008    Kosei Demura