ODE Tutorial 6: Collision Detection

This is 6th tutorial about ODE (Open Dynamics Engine). In this tutorial, we add learn collision detection function on the last tutorial’s sample program.

The source code is shown below. Just the place where it is different from the previous program comment is being inserted. Because only summary it explains concerning API, please read the document of the birthplace web sight of ODE concerning details.

Well, the source code will be read right away. Furthermore, it does not publish the completely same START function as previous sample1.

// sample2.cpp by Kosei Demura
#ifdef dDOUBLE
#define dsDrawSphere dsDrawSphereD

static dWorldID world; // World for kinetics calculation
static dSpaceID space; // Space for collision detection
static dGeomID ground;
static dJointGroupID contactgroup; // As for contact group details ODE manual
dsFunctions fn;
const dReal radius = 0.2, mass = 1.0; // Radius [ m ] and mass [ m ]

// The last time was just body for kinetics calculation, but this time it meaning that geom for collision detection joins it defined ball object with structure.
typedef struct {
dBodyID body; // For kinetics calculation
dGeomID geom; // For collision detection
} MyObject;
MyObject ball; // Ball object

// Call-back function of collision detection
static void nearCallback (void *data and dGeomID o1, dGeomID o2)
static const int N = 4; // As for the upper limit of contact score without forgetting 4 static, attaching.

dContact contact [ N ];

int isGround = ((ground == o1) || (ground == o2));
int n = dCollide (o1, o2, N and &contact [ 0 ] geom, sizeof (dContact)); // As for n collision score
if (isGround) { //the flag of the ground stands, collision detection function can be used
for (int i = 0; I < n; I++) {
contact [ i ] surface.mode = dContactBounce; // Setting the coefficient of rebound of the land
contact [ i ] surface.bounce = 0.0; // (0.0 – 1.0) as for coefficient of rebound from 0 up to 1
contact [ i ] surface.bounce_vel = 0.0; // (0.0 or more) the lowest speed which is necessary for rally

/ / Contact joint formation
dJointID c = dJointCreateContact (world, contactgroup and &contact [ i ]);
// Restraining two geometry which contact with the contact joint
dJointAttach (c, dGeomGetBody (contact [ i ] geom.g1),
dGeomGetBody (contact [ i ] geom.g2));

static void simLoop (int pause)
const dReal *pos and *R;

dSpaceCollide (space,0, &nearCallback); // Collision decision, most first write this.
dWorldStep (world,0.01); // Simulation 1 step is advanced
dJointGroupEmpty (contactgroup); // The joint group is designated as the sky
dsSetColor (1.0,0.0,0.0); // Setting of red
pos = dBodyGetPosition (ball.body); // Position
R = dBodyGetRotation (ball.body); // Revolution queue
dsDrawSphere (pos, R and radius); // Drawing of sphere

void prepDrawStuff () {
fn.version = DS_VERSION;
fn.start = &start;
fn.step = &simLoop;
fn.command = NULL;
fn.stop = NULL;
fn.path_to_textures = “../.. /drawstuff/textures”;

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

prepDrawStuff ();

world = dWorldCreate ();
space = dHashSpaceCreate (0);
contactgroup = dJointGroupCreate (0); // Formation of contact group

dWorldSetGravity (world,0,0, -0.5);

// Create a ground
ground = dCreatePlane (space,0,0,1,0);

// Create a ball
ball.body = dBodyCreate (world);
dMassSetZero (&m1);
dMassSetSphereTotal (&m1, mass and radius);
dBodySetMass (ball.body and &m1);
dBodySetPosition (ball.body and x0, y0, z0);

ball.geom = dCreateSphere (space and radius); // Formation of ball geometry
dGeomSetBody (ball.geom and ball.body); // Setting geom to body

dsSimulationLoop (argc and argv,352,288, &fn);

dWorldDestroy (world);
return 0;

With ODE kinetics calculation and collision detection calculation it is mounted separately. Because this time collision detection function is necessary, first, the space space for collision detection calculation must be made. Object body for kinetics calculation (the body) other things geom (geometry) it is necessary to set in one for the collision detection calculation. With the MyObject structure that the sea urchin defining the member, now the shank.

Geom of ball object dCreateSphere () with is made in main function, it is relation to have attached body and geom with dGeomSetBody and position and attitude of object if sets with just body, are OK. Unless this is done, it falls to 幽 body secession phenomenon.

Opposition sudden detection function dSpaceCollide is called in the simLoop function which is executed with each step of simulation. Please call necessary most with beginning the ず simLoop as the point which notes. When it keeps having this in direction after, the ball penetrates the land. dSpaceCollide, a callback function, is called. When this is explained, being to become too long, this time it makes here.

So,downloading the source code from here, and execute. This time it is the expectation which does not penetrate the land. Manner of execution almost is the same as the last time. Please read sample1 sample2 and change.

1 topics are put out. When sample2 is executed, we like the ground, whether or not is, it is not understood well, but the ball does not bounce completely. Please solve this problem.

See you next time !


  1. demu より:

    Thank you for your comment.

    This article was produced by a translation software.
    I read this. English is so funny.

    Actually, I am not good at English.
    However, I will try to fix these English articles some day.



  2. Aleksander Kozhukhovskiy より:

    Thanks for Eng translation.
    Really good manual with funny errors in tranlation:)
    second day trying to undestand ODE. Very intresting. Always intresting in robotics… but there are no ways to participate… may be numeric calculation – good start for me.

    Thanks for manual, once more…