Game Development Reference

In-Depth Information

status = COLLISION;

pCollisionData++;

NumCollisions++;

}

}

return status;

}

Since
CheckForCollisions
loops through all of the balls checking for collisions with

every other ball, it is possible that a collision would be recorded twice. For example, the

i
th ball may be found to be colliding with the
j
th ball, and later the
j
th ball would also

be found to be colliding with the
i
th ball. We don't want to record that information twice,

so we use the following function to check if a collision between two particular balls is

already recorded. If so, we skip re-recording the data:

bool CollisionRecordedAlready(int i, int j)

{

int k;

int b1, b2;

for(k=0; k<NumCollisions; k++)

{

b1 = Collisions[k].body1;

b2 = Collisions[k].body2;

if( ((b1 == i) && (b2 == j)) ||

((b1 == j) && (b2 == i)) )

return true;

}

return false;

}

Checking ball-table collisions is fairly straightforward as well. If 1) a ball is found to be

headed toward the table with some velocity greater than 0 (or some small threshold),

and 2) the ball's vertical position to its center is less than or equal to its radius, then we

record a collision.
CheckGroundPlaneCollisions
handles this for us:

int CheckGroundPlaneCollisions(pCollision CollisionData, int body1)

{

Vector tmp;

Vector vel1;

Vector pt1;

Vector Vr;

float Vrn;

Vector n;

int status = NOCOLLISION;

if(Bodies[body1].vPosition.z <= (Bodies[body1].fRadius))

{

pt1 = Bodies[body1].vPosition;