Game Development Reference

In-Depth Information

centers of gravity of each colliding object; this is
central impact
, as discussed in
Chap‐

ter 5
, and is the same as that used for the particle example in
Chapter 8
.

Now take a look at the function we've prepared for this simulation to check for collisions:

int CheckForCollision (pRigidBody2D body1, pRigidBody2D body2)

{

Vector d;

float r;

int retval = 0;

float s;

Vector v1, v2;

float Vrn;

r = body1->ColRadius + body2->ColRadius;

d = body1->vPosition - body2->vPosition;

s = d.Magnitude() - r;

d.Normalize();

vCollisionNormal = d;

v1 = body1->vVelocity;

v2 = body2->vVelocity;

vRelativeVelocity = v1 - v2;

Vrn = vRelativeVelocity * vCollisionNormal;

if((fabs(s) <= ctol) && (Vrn < 0.0))

{

retval = 1; // collision;

CollisionBody1 = body1;

CollisionBody2 = body2;

} else if(s < -ctol)

{

retval = −1; // interpenetrating

} else

retval = 0; // no collision

return retval;

}

This function uses a simple bounding circle check to determine whether or not the

hovercraft are colliding. The first thing it does is calculate the distance,
r
, that represents

the absolute minimum separation between these hovercraft when they are in contact.

ColRadius
is the radius of the bounding circle of the hovercraft. We must compute it

for each hovercraft upon initialization as follows:

->ColRadius = SQRT(fLength*fLength + fWidth*fWidth);

Next, the distance separating the hovercraft at the time this function is called is deter‐

mined and stored in the variable
d
. Since we're assuming that these hovercraft are par‐

ticles, determining
d
is simply a matter of calculating the distance between the coordi‐