Collision Detection

If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

Collision detection in 2D graphics is fairly straight-forward. You are normally trying to see whether two rectangular areas are in any way touching or overlapping each other. The rectangles to test for overlapping are the vertical and horizontal extents of the two bitmap images you want to perform collision detection on. A simple implementation of this method, using our existing sprite engine, is as follows:

Going this route usually works fairly well, but it has a major flaw in that for most normal images, it will tend to "overdetect" collision. The problem here is obvious, and Figure 1 shows a classic example. Figure 1 shows two perfect circles - balls let's say - which we want to detect collision on. Because of the roundish shape of the images, the corners of each bitmap contain empty space. But the bounding rectangles of each image are clearly intersecting, so a traditional collision detecting algorithm (like the one listed above) would flag this as a hit.

Figure 1:

This annoys gamers a great deal, and so some game designers decide to define a somewhat smaller rectangle than the full extents of the image, and use this smaller rectangle for the collision detection. A good figure to use is 80 percent of the full bounding box of the image. First, we add four new fields to the sprite data type, called col_width, col_height, col_x_offset, and col_y_offset. During initialization, wherever we are defining our sprite widths and heights, we will also calculate the col_widths and col_heights to be 20% smaller. Then we will define the offset fields to describe where to set the bounding box relative to the object's coordinates. This will look something like this:

This takes a little more processing time per collision, but not much, and the result is a much more convincing collision detection scheme.

But what if we want to really perform collision detection that is perfect - right down to the pixel level? One thing to consider is that often, collision detection is really only needed for a rather small object hitting a rather larger object. In this case, it makes sense to try to decide on a single point to perform collision detection on. Can we do this? Well, yes, but we have to be careful about which pixel to choose for the collision detection. In the case of a very small object, it usually makes sense to choose the central pixel of the image. This can be precalculated during initialization, or it can be built straight into a special collision detection function to be used only for these small objects. Generally, in a complex game, there may be several different collision detection routines used for different situations.

So assuming that we have an object and a point to test for collision, we can compose a new collision detector which will test at the pixel level:

The key to understanding this method is that bitmap images are stored with the assumption that the transparent color (black) is conventionally represented by zero. So we first set up the left, right, top, and bottom temporary variables, and then proceed to do a standard test to see if the passed point is within the object's bounding box. Note that this time, we want to test for collision on the entire bounding box, not a reduced rectangle. Also note how this is done: we are actually testing for *no collision*, and returning false (0) if any of the trivial rejection tests prevail. This is important for speed. Note that the x dimension is tested first: this is because all graphical modes are wider than they are tall, and so if a point is not colliding with a certain object, it's more likely to be because it's too far away from the object in the horizontal dimension than the vertical dimension. We want to test the more frequent occurrences first to avoid unnecessary tests further into the procedure.

Once it has been established that the point is definitely within the bounding box of the object, we use the coordinates of the point and object to calculate an offset into the bitmap data of the object. The idea here is to see if the single point is on top of a non-transparent pixel in the bitmap image of the object. To do this, we need to do some math to figure out where the point is relative to the object's image. Once we have this offset, we simply test to see if the bitmap pixel at that offset is black (0), or some actual bitmap data. This offset calculation, and the fact that we are only testing for a single point of collision, is the key to fast pixel-level collision detection.

Another thing to notice is that when calculating the offset into the bitmap data, I chose to take the absolute value of the results. This means that this collision detection routine can be run safely (and with meaningful results) on objects that are not necessarily within the screen boundaries.

This is great for testing single-point, pixel-level collisions, and it works very well for detecting very small objects (bullets, missiles, etc.) hitting larger game objects (alien or enemy ships, dragons, or whatever). But what if we need to detect two full-sized objects for collision, and it really needs to happen at the pixel level?

Well, we must resort to a more expensive algorithm. In this case, what we have to do is first establish whether or not the full bounding boxes of each object are overlapping in any way. We know how to do that. Once we've determined that they do overlap, we need to compute the actual area of overlap between the two bounding boxes. This will be a new rectangle, and once we have it, we can start scanning it pixel at a time, checking to see if the pixel in question is non-zero in both the object's bitmaps. If so, we can stop immediately and declare a collision. If not, we must continue scanning until we reach the last possible pixel location within the rectangle of overlap. Only then can we declare that no collision occurred.

As you might imagine, this is rather lengthy, and takes a lot more execution time. The code for this type of collision detection is as follows:

Because it runs so long, this type of collision detection should probably be minimized in your game implementations. You may want to determine at run-time which objects need full, object-to-object pixel-level collision detection and which objects can get away with one of the cheaper varieties. The best way to determine this is usually by good old-fashioned experimentation.

This last algorithm can be sped up substantially by a number of methods including loop-flipping, non-indexed loops, and possibly loop unrolling. Also, the increment for pixel1 and pixel2 at the bottom of the outer loop can be precomputed.

Well, that's it for this time. Next maybe we'll talk a little about 3D collision detection.