Free MPEG Video Example
Click here to go to the MPEG Video example

ODE Bouncing Ball
 

By Alan Baylis 06/04/2006



Those who have been following the ODE tutorial from the start will have experimented on their own and will probably find this little tutorial a bit boring. But for those new to the tutorials I thought I would cover how to create a spherical object that bounces. The InitODE routine is very similar to that of the first tutorial in that it only initializes a single object. I will only cover the second part of the InitODE routine here as the first part hasn't changed.

// Set the radius of our sphere object
dReal radius = 0.5;
dMass m;

Object.Body = dBodyCreate(World);

dMatrix3 R;
dBodySetPosition(Object.Body, 0, 10, -5);
VECTOR tempVect(0.0, 0.0, 0.0);
dBodySetLinearVel(Object.Body, tempVect.x, tempVect.y, tempVect.z);
dRFromAxisAndAngle(R, 1, 0, 0, -1.57);
dBodySetRotation(Object.Body, R);

// Here we use dMassSetSphere instead of dMassSetBox and we pass the local radius variable as the third parameter
dMassSetSphere(&m, DENSITY, radius);

// To create the sphere object we use dCreateSphere and pass it the same local radius variable
Object.Geom[0] = dCreateSphere(Space, radius);
dGeomSetBody(Object.Geom[0], Object.Body);
dBodySetMass(Object.Body, &m);

As you can see there isn't much difference between creating a sphere object and a rectangular object. In order to make the sphere bounce though we must change the contact parameters in the nearCallback routine.

contact[i].surface.bounce = 0.9;
contact[i].surface.bounce_vel = 0.5;



I have set the bounciness of the contact to 0.9 to make the ball very bouncy, if you set this to a value greater than 1.0 then the ball will bounce higher than the position it was dropped from. Of course this looks very unnatural and you probably will want to use values less than 1.0 in your simulation. The second contact parameter sets the velocity at which the contacting objects will seperate, which I have set to 0.5.

I noticed that in the earlier examples that the simulation would appear to lock up after a while, this was due to setting the dWorldSetAutoDisableFlag to true. If you don't want the objects in the simulation to be disabled when they come to rest then you can set this flag to false.

dWorldSetAutoDisableFlag(World, 0);

Another way to prevent objects that have been disabled from being passed over in the simulation is to use dBodyEnable on the objects before you change them as I have done when the space bar is pressed.

dBodyEnable(Object.Body);
VECTOR tempVect(0.0, 3.0, 0.0);
dBodySetLinearVel(Object.Body, tempVect.x, tempVect.y, tempVect.z);

In this example I have applied a force that pushes up beneath the sphere using dBodySetLinearVel and you can experiment with different vector directions to see the effect on the sphere. I hope that the information in this tutorial helps you to add spheres and bouncing objects to your own simulations. In the next tutorial I will cover the subject of adding constraints/stops to restrict the available movement of common joints. See you there.

Alan Baylis



Above Content Copyright © 1998 - 2005 Alan Baylis, All Rights Reserved