15. Interactive Simulation

This is the last ODE (Open Dynamics Engine) tutorial. Thank you very much for reading so far.

The last tutorial explains how to make an interactive simulation. You can use a keyboard to control objects in ODE. To realize that you have to make a command function which defines keys how to control objects.

Keyboard operation

The following sample code show the command function.  You can add force to objects and restart the simulation.

void  prepDrawStuff()
  fn.version = DS_VERSION;
  fn.start   = &start;
  fn.step    = &simLoop;
  fn.command = &command; // command function for key input
  fn.stop    = NULL;
  fn.path_to_textures = "../../drawstuff/textures";

/*** Command function is called by key input ***/
void command(int cmd)
  switch (cmd) {
    case ’s’: dBodyAddForce(pillar.body,50, 0, 0);break; // When s key is pressed
    case ‘j’: dBodyAddForce(ball.body ,0,-10, 0);break; // When j key is pressed   
    case ‘k’: dBodyAddForce(ball.body,0,  0,50);break; // When k is pressed
    case ‘l’: dBodyAddForce(ball.body,0, 10, 0);break; // When l is pressed
    case ‘r’:restart();break;   // When the 'r' key is pressed

/***  Restart simulation ***/
void restart()
  // Destroy
  dJointGroupDestroy(contactgroup); // Destroy a joint group
  destroyBall();    // Destroy a ball  
  destroyCylinder(); // Destroy a cylinder

  // Create
  contactgroup = dJointGroupCreate(0); // Create a new contact group
  createBall();   // Create a new ball
  createPillar();  // Create a new cylinder

/*** Destroy a ball ***/
void destroyBall()

void destroyCylinder()

static void simLoop (int pause)
  if (!pause) {

The command function processes the key inputs. The command function realizes an interactive simulation. In this case, the ‘s’, ‘ j’ , ‘k’ , ‘ l’ keys add force the cylinder geometry.


If the  ‘r’ key is pressed, then restart a simulation. However, if you want to restart the simulation,  you must do some necessary processes. In the sample program,  to simplify, destroy the body and the geometry and rebirth them.

  • Delete the contact joint
  • Reset the position and posture of bodies.
  • Reset the velocity and angular velocity of the bodies.
  • If you control joints, reset the target velocity and the torque.

The source code, sample15.cpp,  can be downloaded from here.

Please download and enjoy it .

Thank you very much again for reading this ODE tutorials.


demu (Kosei Demura)