Game Development Reference
In-Depth Information
is penetration or until the time step has been reduced to a size small enough to force an
exit to the loop. The purpose of this looping is to find the largest step size, less than or
equal to dt , that can be taken and still avoid penetration. You either want a collision or
no collision.
You might ask yourself when does small become too small in terms of time step? Too
small is obviously when the time step approaches 0 and your entire simulation grinds
to a halt. Therefore, you may want to put in some criteria to exit this loop before things
slow down too much. This is all subject to tuning, by the way, and it also depends on
the value you set for ctol . We can't stress enough the importance of tuning these pa‐
rameters. Basically, you must strive for visual realism while keeping your frame rates
up to required levels.
Looking inside this while loop reveals what's going on. First, tryAgain is set to false ,
optimistically assuming that there will be no penetration, and we make copies of the
hovercraft's states, reflecting the last successful call to UpdateSimulation .
Next, we make the usual call to UpdateBody for each copy of the hovercraft. Then a call
to the collision detection function, CheckForCollision , is made to see if Craft is col‐
liding with or penetrating Craft2 . If there is penetration, then tryAgain is set to true ,
dtime is cut in half, didPen is set to true , and the function takes another lap through
the while loop. didPen is a flag that lets us know that a penetration condition did occur.
If there was a collision, the function handles it by applying the appropriate impulse:
if(CollisionBody1 != 0 && CollisionBody2 != 0)
ApplyImpulse(CollisionBody1, CollisionBody2);
After getting through the while loop, the updated hovercraft states are saved and Upda
teSimulation is complete.
The last bit of code you need to add includes a few new global variables and define s:
#define LINEARDRAGCOEFFICIENT 0.25f
#define COEFFICIENTOFRESTITUTION 0.5f
#define COLLISIONTOLERANCE 2.0f
Vector vCollisionNormal;
Vector vRelativeVelocity;
float fCr = COEFFICIENTOFRESTITUTION;
float const ctol = COLLISIONTOLERANCE;
The only one we haven't mentioned so far, although you've seen it in ApplyImpulse , is
fCr , the coefficient of restitution. Here we have it set to 0.5, which means that the
collisions are halfway between perfectly elastic and perfectly inelastic (refer back to our
earlier discussions on coefficients of restitution in Chapter 5 if you've forgotten these
terms). This is one of those parameters that you'll have to tune to get the desired be‐
havior.
Search Nedrilad ::




Custom Search