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

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.