Game Development Reference

In-Depth Information

Excerpt from src/collide_fine.cpp

// Early-out check to see if we can exclude the contact.

if (real_abs(relCenter.x) - sphere.radius > box.halfSize.x ||

real_abs(relCenter.y) - sphere.radius > box.halfSize.y ||

real_abs(relCenter.z) - sphere.radius > box.halfSize.z)

{

return 0;

}

F
IGURE
13.13

Separating axes between a box and a sphere.

Contact Generation

The final phase of our algorithm is to find the closest point in the box to the target

point and generate the contact from it. We'll do this first in the coordinates of the box.

This is a simple process. All we need to do is to clamp each component of the test

point to the half-size of the box in the same direction. With this new point we can

then work out the distance from the center of the sphere to the target point, and exit

if it is larger than the radius of the sphere. The code for this is simple:

Excerpt from src/collide_fine.cpp

Vector3 closestPt(0,0,0);

real dist;

// Clamp each coordinate to the box.