Game Development Reference
In-Depth Information
// If we don't have any penetration, skip this step.
if (penetration <= 0) return;
// The movement of each object is based on its inverse mass, so
// total that.
real totalInverseMass = particle[0]->getInverseMass();
if (particle[1]) totalInverseMass += particle[1]->getInverseMass();
// If all particles have infinite mass, then we do nothing.
if (totalInverseMass <= 0) return;
// Find the amount of penetration resolution per unit of inverse mass.
Vector3 movePerIMass = contactNormal *
(-penetration / totalInverseMass);
// Apply the penetration resolution.
particle[0]->setPosition(particle[0]->getPosition() +
movePerIMass * particle[0]->getInverseMass());
if (particle[1])
{
particle[1]->setPosition(particle[1]->getPosition() +
movePerIMass * particle[1]->getInverseMass());
}
}
We now have code to apply the change in velocity at a collision and to resolve objects
that are interpenetrating. If you implement and run the contact resolution system, it
will work well for medium-speed collisions, but objects resting (a particle resting on a
table, for example) may appear to vibrate and may even leap into the air occasionally. 1
To have a complete and stable contact resolution system we need to reconsider
what happens when two objects are touching but have a very small or zero closing
velocity.
1. I said medium-speed here because very high-speed collisions are notoriously difficult to cope with.
The physics simulation we've provided will usually cope (except for insanely high speeds where a lack
of floating-point accuracy starts to cause problems), but collision detectors can start to provide strange
results: it is possible for two objects to pass right through each other before the collision detector realizes
they have even touched. If it does detect a collision, they may be at least halfway through each other and be
separating again, in which case they have a positive separating velocity and no impulse is generated. We'll
return to these issues when we create our collision detection system later in this topic, although we will not
be able to resolve them fully: they are a feature of very high-speed collision detection.