Geometrical Collision Detection
Method I: Bounding Spheres
A first and rather naive method to test whether two polygons are intersecting, is to embed each polygon into a circle, that is, each polygon is assumed to have an average radius and if these radii overlap, the polygons are said to collide. As an example, look at this spaceship from the Empire racing head-on against a strange asteroid:
Centroids and Radii
To implement a bounding sphere algorithm, it is thus necessary to be able to compute the centroid of a polygon. Read the The Centroid of Convex Polygons article for an explanation on how to compute the centroid of convex polygons (the case of non-convex polygons is similar, more care has to be taken when triangulating the polygon though).
There are many choices for the radii, one can simply take the largest distance between the centroid of the polygon and its vertices (as in the figure above), one can take the distance of each vertex and average these radii, or one can just take some other heuristic. André LaMothe, the author of the book Tricks of the Windows Game Programming Gurus, mentions that he likes to use a value that is midway between the average and the farthest vertex. Let us try that out: The average distance for the above asteroid is
As illustrated by the figure belows, putting a circular bounding box around polygons will lead to the detection of collisions when there are none, as well as missed collisions.
Distance and Square Roots
On modern computers, calculating the distance between two points in the Euclidean plane is as easy as computing a square root: Let
In C++ this looks as follows:
Bear in mind, however, that on earlier hardware, the square root computation took a lot of time. If you want to program a game for older hardware, have a look at the Fast Approximate Distance article, by Rafael Baptista, which explains how to approximate the square root function by a linear combination of the minimum and maximum functions.
Checking for collisions is now as easy as testing a single equation: Let
Please note that in light of the discussion about fast distance functions, it is commonly not even necessary to compute the actual distance between two points. For example, by squaring the above equation, one gets:
A theoretical, and naive, bounding sphere collision detection algorithm between two polygons would thus look like this:
Method II: Bounding Boxes
As seen above, approximating a polygonal object by a sphere doesn’t always work very well. Another approach would be to approximate the polygon by a rectangular box containing the entire polygon. To do so, computing the four edges of a rectangle, by finding the furthest reaches of the polygon, is enough:
From the above figure, one can conclude that bounding boxes can offer a better approximation as bounding spheres, simply due to their geometrical shape being closer to the shape of most polygons. Both failed collision tests with the bounding sphere algorithm from above would be avoided by the bounding box test in this case:
False positives are still a nuisance, however:
In C++, the code to compute a bounding box is straightforward:
Figuring out if a point is within a rectangle is now trivial: Given a rectangular bounding box by
Thus to detect collisions with bounding boxes, it would now be sufficient to, for example, test any of the four corners of a box against another bounding box, or, if time permits, by using more clever methods. We won’t cover any of these in detail, however, until in a later tutorial.
The easiest method is to test for those cases where two bounding boxes definitely can’t intersect, i.e. let
References
- Geogebra
- Tricks of the Windows Game Programming Gurus, by André LaMothe
- Wikipedia