Game Development Reference

In-Depth Information

n.z = 1;

Vr = vel1;

Vrn = Vr * n;

if(fabs(Vrn) <= VELOCITYTOLERANCE) // at rest

{

// Check the relative acceleration:

Ar = Bodies[body1].vAcceleration +

(Bodies[body1].vAngularVelocityGlobal ^

(Bodies[body1].vAngularVelocityGlobal^pt1)) +

(Bodies[body1].vAngularAccelerationGlobal ^ pt1);

Arn = Ar * n;

if(Arn <= 0.0f)

{

// We have a contact so fill the data structure

assert(NumCollisions < (NUMBODIES*8));

if(NumCollisions < (NUMBODIES*8))

{

CollisionData->body1 = body1;

CollisionData->body2 = −1;

CollisionData->vCollisionNormal = n;

CollisionData->vCollisionPoint = tmp;

CollisionData->vRelativeVelocity = Vr;

CollisionData->vRelativeAcceleration = Ar;

CollisionData->vCollisionTangent = (n^Vr)^n;

CollisionData->vCollisionTangent.Reverse();

CollisionData->vCollisionTangent.Normalize();

CollisionData++;

NumCollisions++;

status = CONTACT;

}

}

}

}

return status;

}

That's all there is to this billiards example. As you can see, we used substantially the

same methods shown in other examples throughout this topic to implement this ex‐

ample. About the only new information we've shown here is how to compute rolling

resistance. With a little effort, you can combine the material presented in this example

with the projectile motion material presented in
Chapter 6
to model all sorts of sports

balls. Whether you're modeling a billiard ball bouncing off a table or a basketball

bouncing off a backboard, the methods are the same. The only things that will change

are the empirical coefficients you use to model each ball and surface. Have fun.