Game Development Reference
In-Depth Information
Excerpt from src/contacts.cpp
// Build a vector that shows the change in velocity in
// world space for a unit impulse in the direction of the contact
// normal.
Vector3 deltaVelWorld = relativeContactPosition[0] % contactNormal;
deltaVelWorld = inverseInertiaTensor[0].transform(deltaVelWorld);
deltaVelWorld = deltaVelWorld % relativeContactPosition[0];
// Work out the change in velocity in contact coordinates.
real deltaVelocity = deltaVelWorld * contactNormal;
// Add the linear component of velocity change.
deltaVelocity += body[0]->getInverseMass();
// Check whether we need to consider the second body's data.
if (body[1])
{
// Find the inertia tensor for this body.
body[1]->getInverseInertiaTensorWorld(&inverseInertiaTensor[1]);
// Go through the same transformation sequence again.
Vector3 deltaVelWorld = relativeContactPosition[1] % contactNormal;
deltaVelWorld = inverseInertiaTensor[1].transform(deltaVelWorld);
deltaVelWorld = deltaVelWorld % relativeContactPosition[1];
// Add the change in velocity due to rotation.
deltaVelocity += deltaVelWorld * contactNormal;
// Add the change in velocity due to linear motion.
deltaVelocity += body[1]->getInverseMass();
}
In this code the first body is considered. Its rotational component of velocity change
is calculated and placed in the deltaVelocity component, followed by its linear com-
ponent. If a second body is present in the contact, then the same process is repeated,
and the deltaVelocity is incremented with the two components for body 2. At the
end of the process deltaVelocity contains the total velocity change per unit impulse.
14.2.3
I MPULSE C HANGE BY V ELOCITY
For frictionless collisions this step is incredibly simple. If we have a single value for
the velocity change per unit impulse (call it d ), then the impulse needed to achieve