Game Development Reference
real deltaVelocity = -contactVelocity.x -
(contactVelocity.x - velocityFromAcc);
Making this simple adjustment reduces the amount of visual vibration for objects
resting on the ground. When objects are in tight groups, such as stacks, the vibra-
tion can return. To solve that problem we'll perform the second step—reducing the
coefficient of restitution.
We'll return to the velocity caused by acceleration later in this chapter. We will
need another calculation of this kind to solve a problem with friction at resting con-
L OWERING THE R ESTITUTION
The change we made in the previous section effectively reduces the restitution at con-
tacts. Before reducing the velocity we have collisions with greater separating velocity
than closing velocity: the objects are pushed apart even when they begin resting. This
occurs when there is a coefficient of restitution above 1. The smaller the coefficient,
the less bounce there will be.
When the acceleration compensation alone doesn't work, we can manually lower
the coefficient of restitution to discourage vibration. This can be done in a very simple
real appliedRestitution = restitution;
if (contactVelocity.magnitude() < velocityLimit)
appliedRestitution = (real)0.0f;
We could use a more sophisticated method, where the restitution is scaled so that
it is smaller for smaller velocities, but the version here works quite well in practice.
If you see visible transitions between bouncing and sticking as objects slow down,
try reducing the velocity limit (I use a value of around 0.1 in my engine). If this
introduces vibration, then the scaling approach may be useful to you.
T HE N EW V ELOCITY C ALCULATION
Combining both techniques for resting contacts, we end up with the following code
in our adjustVelocities method: