O'Reilly logo

Learning XNA 3.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

To implement ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required