Game Development Reference

In-Depth Information

void detectContacts(const Primitive &firstPrimitive,

const Primitive &secondPrimitive,

CollisionData *data);

where the
Primitive
type holds the data for the collision geometry. The
Primitive

class holds data that any contact generator will need to know, such as the rigid body

corresponding to the geometry and the offset of the primitive from the coordinate

origin of the rigid body.

class Primitive

{

public:

RigidBody *body;

Matrix4 offset;

};

Throughout this chapter I will assume that the offset matrix represents transla-

tion and rotation only: it has no scaling or skewing effect. We could make this much

simpler and assume that the primitive is aligned perfectly with the center of the rigid

body with no rotation (as would be the case if we had a cylinder representing a bar-

rel, for example). Unfortunately this would not work for assemblies of objects or for

rigid bodies with centers of mass that aren't at their geometric center. For flexibility it

is best to allow primitives to be offset from their rigid bodies.

Each implemented contact generation function will use a subtype of
Primitive

with some additional data (such as
Sphere
and
Plane
). I'll introduce these types as we

go along.

13.3.1

C
OLLIDING
T
WO
S
PHERES

Colliding two spheres is as simple as it gets. Two spheres are in contact if the distance

between their centers is less than the sum of their radii.

If they are in contact, then there will be precisely one contact point: each sphere

consists of one surface, so it will be a face-face contact (see figure 13.8).

The point of deepest contact is located along the line between the sphere cen-

ters. This is exactly the same algorithm we saw in chapter 7 when looking at particle

collisions.

To implement this we need a data structure for a sphere. Spheres are completely

defined by their center point and radius:

class Sphere

{

public: