Collision Detection

So, you've got a pretty good thing going thus far. Players can interact with your game and move the three rings around the screen—but still there's not a lot to do. You need to add some collision detection in order to take the next step.

Collision detection is a critical component of almost any game. Have you ever played a shooter game where you seem to hit your target but nothing happens? Or a racing game where you seem to be far away from a wall but you hit it anyway? This kind of gameplay is infuriating to players, and it's a result of poorly implemented collision detection.

Collision detection can definitely make or break a gameplay experience. The reason it's such a make-or-break issue is because the more precise and accurate you make your collision-detection algorithms, the slower your gameplay becomes. There is a clear trade-off between accuracy and performance when it comes to collision detection.

One of the simplest and fastest ways to implement collision detection is through the bounding-box algorithm. Essentially, when using a bounding-box algorithm, you "draw" a box around each object on the screen and then check to see whether the boxes themselves intersect. If they do, you have a collision. Figure 3-3 shows the three rings and skull ball sprites with these invisible boxes surrounding the two objects.

Bounding boxes around your objects

Figure 3-3. Bounding boxes around your objects ...

Get Learning XNA 3.0 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.