Game Development Reference
F IGURE 14.11
Resolution order is significant.
Contact* lastContact = contacts + numContacts;
for(Contact* contact=contacts; contact < lastContact; contact++)
This will work but isn't optimal.
Figure 14.11 shows three interpenetrating contacts in a row. The middle part of
the figure shows what happens when the contacts are resolved in order: a large in-
terpenetration remains. The final part of the figure shows the same set of contacts
after resolving in reverse order. There is still some interpenetration visible, but it is
Rather than go through the contacts in order and resolve their interpenetration,
we can resolve the collisions in penetration order. At each iteration we search through
to find the collision with the deepest penetration value. This is handled through its
applyPositionChange method in the normal way. The process is then repeated up to
some maximum number of iterations (or until there are no more interpenetrations
to resolve, whichever comes first).
This algorithm can revisit the same contacts several times. Figure 14.12 shows a
box resting on a flat plane. Each corner is penetrating the surface. Moving the first
corner up will cause the second to descend farther. Moving the second will cause