Nov 162013
 

Today, lets see how to detect two sprites (i.e moving textures) colliding.
Pixel collision is the basic of any game.

The principle is the following :
first we need to define the region the two image rectangles intersect,
then we will iterate through each pixel in that region, and compare the alpha value of each image of each pixel.
If neither has an alpha value of 0 (transparent), they are both considered colliding.
See original discussion here.

A picture illustrating this priniciple :
xna_pixel_collision

In the attached code, we will
-setup a background and 2 balls
-update the balls position and make a sound when we touch the screen boundaries
-detect if the balls are colliding and if yes, make a sound

Note : this does not cover rotating shapes as this will be covered in a future article.

Enjoy.

xna_demo_6

 Posted by at 12 h 39 min

Poster un Commentaire

avatar
  S’abonner  
Notifier de