Game Development Reference
In-Depth Information
double mag = m.Magnitude();
Vector a = Bodies[i].vAngularVelocity;
Bodies[i].vMoments += -a * mag;
Handling Collisions
Earlier you saw where StepSimulation makes a few function calls to deal with collision
checking and response. You also saw where CalcObjectForces makes a function call
that checks for contacts. The functions that check for collisions or contacts make use of
the Collisions array we showed you earlier. This array stores all the relevant informa‐
tion pertaining to collisions or contacts—the collision or contact manifold, normal and
tangent vectors, relative velocity, etc.
The first function we'll consider is CheckForCollisions , which is called toward the end
of StepSimulation . CheckForCollisions checks for ball-ball collisions; we have a sep‐
arate function to check for ball-table collisions that we'll get to later. CheckForColli
sions relies on concepts we've already discussed and showed you in earlier chapters, so
we'll summarize its action here. Basically, two billiard balls are colliding if 1) they are
headed toward each other, and 2) the distance separating their centers is less than or
equal to the sum of their radii. If both of these criteria are met, then a collision is recorded
and all relevant data is stored in the Collisions array:
int CheckForCollisions(void)
int status = NOCOLLISION;
int i, j;
Vector d;
pCollision pCollisionData;
int check = NOCOLLISION;
float r;
float s;
Vector tmp;
pCollisionData = Collisions;
NumCollisions = 0;
// check object collisions with each other
for(i=0; i<NUMBODIES; i++)
for(j=0; j<NUMBODIES; j++)
if((j!=i) && !CollisionRecordedAlready(i, j))
// do a bounding sphere check
d = Bodies[i].vPosition - Bodies[j].vPosition;
r = Bodies[i].fRadius + Bodies[j].fRadius;
s = d.Magnitude() - r;
Search Nedrilad ::

Custom Search