Game Development Reference
In-Depth Information
Contact* lastContact = contacts + numContacts;
for (unsigned i = 0; i < positionIterations; i++)
Contact* worstContact = NULL;
real worstPenetration = 0;
for(Contact* contact = contacts; contact < lastContact; contact++)
if (contact->penetration > worstPenetration)
worstContact = contact;
worstPenetration = contact->penetration;
if (worstContact) worstContact->applyPositionChange();
else break;
This method looks through the whole list of contacts at each iteration. If this were
all we needed, then we could do better by sorting the list of contacts first, and then
simply work through them in turn.
Unfortunately the preceding algorithm doesn't take into account the fact that
one adjustment may change the penetration of other contacts. The penetration data
member of the contact is set during collision detection. Movement of the objects
during resolution can change the penetration depth of other contacts, as we saw in
figures 14.9 and 14.12.
To take this into account we need to add an update to the end of the algorithm:
for (unsigned i = 0; i < positionIterations; i++)
// Find worstContact (as before) ...
if (!worstContact) break;
where updatePenetrations recalculates the penetrations for each contact. To imple-
ment this method accurately we'd need to go back to the collision detector and work
out all the contacts again. Moving an object out of penetration may cause another
Search Nedrilad ::

Custom Search