A Basic Collision Detection

One of the basic things that virtually all games have is a collision detection method. In this case, all you need to determine is whether a particular moveable object is colliding with the level. (See Listing 14.12.)

Listing 14.12. Collision Detection Algorithm
 public bool HitTest(IMoveableObject obj, out Plane hitPlane) { // Default to an empty plane hitPlane = Plane.Empty; // This object hasn't left the level, has it hit a wall? IntersectInformation closestHit = new IntersectInformation(); if (levelMesh.Intersect(obj.Position, obj.Direction, out closestHit)) { // This will always return true since you will always be hitting the // outside of the level. Now you need to check to see if you're // close enough to really ...

Get Beginning 3D Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.