Game Development Reference
In-Depth Information
{
tryAgain = false;
memcpy(&craft1Copy, &Craft, sizeof(RigidBody2D));
memcpy(&craft2Copy, &Craft2, sizeof(RigidBody2D));
Craft.UpdateBodyEuler(dtime);
Craft2.UpdateBodyEuler(dtime);
CollisionBody1 = 0;
CollisionBody2 = 0;
check = CheckForCollision(&craft1Copy, &craft2Copy);
if(check == PENETRATING)
{
dtime = dtime/2;
tryAgain = true;
didPen = true;
} else if(check == COLLISION)
{
if(CollisionBody1 != 0 && CollisionBody2 != 0)
ApplyImpulse(CollisionBody1, CollisionBody2);
}
}
if(!didPen)
{
memcpy(&Craft, &craft1Copy, sizeof(RigidBody2D));
memcpy(&Craft2, &craft2Copy, sizeof(RigidBody2D));
}
}
Obviously, this version is more complicated than the original version. There's one main
reason for this: penetration could occur because the hovercraft can move far enough
within a single time step to become overlapped. Visually, this situation is unappealing
and unrealistic, so you should to try to prevent it.
The first thing this function does is enter a while loop:
while(tryAgain && dtime > tol)
{
.
.
.
}
This loop is used to back up the simulation if penetration has occurred on the initial
time step. What happens is this: the function first tries to update the hovercraft and then
checks to see if there is a collision. If there is a collision, then it gets handled by applying
the impulse. If there is penetration, however, then you know the time step was too big
and you have to try again. When this occurs, tryAgain is set to true , the time step is
cut in half, and another attempt is made. The function stays in this loop as long as there
Search Nedrilad ::




Custom Search