Game Development Reference
Excerpt from src/collide_fine.cpp
const Sphere &sphere,
const Plane &plane,
// Make sure we have contacts.
if (data->contactsLeft <= 0) return 0;
// Cache the sphere position.
Vector3 position = sphere.getAxis(3);
// Find the distance from the plane.
real ballDistance =
plane.direction * position -
sphere.radius - plane.offset;
if (ballDistance >= 0) return 0;
// Create the contact - it has a normal in the plane direction.
Contact* contact = data->contacts;
contact->contactNormal = plane.direction;
contact->penetration = -ballDistance;
position - plane.direction * (ballDistance + sphere.radius);
// Write the appropriate data.
contact->body = sphere.body;
contact->body = NULL;
contact->restitution = data->restitution;
contact->friction = data->friction;
Strictly speaking, this isn't a sphere-plane collision but a sphere-half-space col-
lision. Figure 13.9 shows the difference. Planes are rarely needed in a game (because
they are infinitely big), but half-spaces are common. They are normally used in games
to represent the ground and walls as part of a BSP tree.
To modify the algorithm to perform true plane-sphere collisions we need to check
whether the distance is either greater than the radius of the sphere or less than the
negative of that radius.