Game Development Reference
In-Depth Information
Excerpt from src/collide_fine.cpp
unsigned CollisionDetector::sphereAndHalfSpace(
const Sphere &sphere,
const Plane &plane,
CollisionData *data
)
{
// 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;
contact->contactPoint =
position - plane.direction * (ballDistance + sphere.radius);
// Write the appropriate data.
contact->body[0] = sphere.body;
contact->body[1] = NULL;
contact->restitution = data->restitution;
contact->friction = data->friction;
return 1;
}
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.
Search Nedrilad ::




Custom Search