Game Development Reference
Calculating the Desired Velocity Change
As I mentioned at the start of the chapter, the velocity change we are looking for is
given by the same equation we used for particles:
v s =−
In other words, we need to remove all the existing closing velocity at the contact, and
keep going so that the final velocity is c times its original value but in the opposite
direction. In code this is simply
real deltaVelocity = -contactVelocity.x * (1 + restitution);
If the coefficient of restitution, c , is zero, then the change in velocity will be suf-
ficient to remove all the existing closing velocity but no more. In other words, the
objects will end up not separating. If the coefficient is near 1, the objects will separate
at almost the same speed at which they were closing.
The value of the coefficient depends on the materials involved in the collision.
Values around 0.4 look visibly very bouncy, like a rubber ball on a hard floor. Values
above this can start to look unrealistic.
C ALCULATING THE I MPULSE
With the desired velocity change in hand, the impulse is given by equation 14.3.
Because we are not concerned with friction, we are only concerned with the im-
pulse in the direction of the contact normal. In contact coordinates the contact nor-
mal is the X axis, so the final impulse vector is
g contact =
where g is the impulse, as earlier. This is implemented as
Excerpt from src/contacts.cpp
// Calculate the required size of the impulse.
impulseContact.x = desiredDeltaVelocity / deltaVelocity;
impulseContact.y = 0;
impulseContact.z = 0;
At this stage it is convenient to convert out of contact coordinates into world
coordinates. This makes applying the impulse in the final stage simpler. We can do
this using our basis matrix to change coordinates:
g world =
M g contact