In a 2D game, a general method for detecting collision is by using bounding box. This is the solution for a lot of situations where precision is not the most important factor. However, if your game cares whether two irregular objects collide with each other or overlap, the bounding box will not be comfortable with the. At this moment, per pixel collision will help you. In this recipe, you will learn how to use this technique in your game.
1. Create a Windows Phone Game project named
PixelCollision2D
, changeGame1.cs
toPixelCollision2DGame.cs
. Then, add thePixelBall.png
andPixelScene.png
file to the content project.2. Add the indispensable data members to the field of
PixelCollision2DGame
.// SpriteFont object SpriteFont font; // The images we will draw Texture2D texScene; Texture2D texBall; // The color data for the images; used for per pixel collision Color[] textureDataScene; Color[] textureDataBall; // Ball position...