Basically, if I have an array along the lines of

    bool[] collision;

Filled with a value for each pixel in the sprite I am checking collision against (pixel 1 = true, pixel 2 = false etc), how do I use it to check collision between the player and the texture?

      protected void GetCollisionData()
    // Declare an array to hold the pixel data 
        uint[] pixelLevelData = new uint[textureLevel.Width * textureLevel.Height];
        // Populate the array 
        textureLevel.GetData(pixelLevelData, 0, textureLevel.Width * textureLevel.Height);

        for (int x = 0; x < textureLevel.Width; x++)
            for (int y = 0; y < textureLevel.Height; y++)
                 if (x < textureLevel.Width && x > 0 && y > 0 && y < textureLevel.Height)
                    //If it's black, make a note that collision will happen
                    if (pixelLevelData[(x + y * textureLevel.Width)] == 4278190080)                        
                        collision[x + y * textureLevel.Width] = true;
                        debug = 9;
                    //else it's either something black already covers or transparent; no collision necessary
                        collision[x + y * textureLevel.Width] = false;

This is the relevant part that covers how I got my array of bools. How to I check collision between the set of bools and a normal rectangle?

  • 2
    \$\begingroup\$ Could you explain on why you are doing this? What are you using this for? \$\endgroup\$
    – user55564
    Dec 7, 2014 at 17:37

1 Answer 1


What you are trying to do is called 'pixel perfect collision detection'. Both player and texture should be inside a rectangle. Everytime Update function is called you should check "broad collision" to see if the two rectangles intersect (there is a Rectangle.Intersects method in XNA for rectangles) . If they do intersect, we start checking "detailed collision". To do that you will have to check the intersection rectangle (the small area where the two rectangles collide) pixel per pixel, to see if each two pixels are both not black. If they are both not black, then there is an actual collision.
Useful link
More Useful link

On a sidenote: In most cases pixel per pixel collision detection is not needed and its really slow. Usually you use polygons/circles/rectangles/edges to check the collisions
enter image description here

In physics engines, there are classes and methods that can create those polygons/circles/rectangles/edges. If those shapes are not enough, there are also some algorithms inside physics engines (I have used Farseer Physics Engine with Monogame) which can create polygons from image "automatically" (depending on where the area is transparent or not usually), like Mark Bayazit's Algorithm. Every algorithm has differences, for example one can detect "holes" inside the texture, others can't.
Example: enter image description here

When it comes to collision detection, you are better off using a physics engine or you will end up learning math/physics/trigonometry instead of programming and basically recreating a physics engine yourself.

Also, collision detection can be problematic if not done right. Let's say you have two objects that you want to check collision in every frame, a moving ball and a static thin wall. Unless you are moving the ball 1 pixel per frame, the following problems will happen:

  • The ball will eventually end up being inside the wall at the frame when the collision is happening
  • At really high ball speeds, the collision will never happen, because the ball will pass right through the wall, so the ball and the wall will never intersect (this is called tunneling)
  • \$\begingroup\$ Please only make edits if you have a significant change to make. You've made a lot of minor edits to this post, so either save them up for one edit or skip them. Continually editing keeps pushing the question and answer to the top of the recent queue and it shouldn't be there for minor edits. \$\endgroup\$
    – House
    Jan 23, 2015 at 15:11
  • \$\begingroup\$ @Byte56 thanks for pointing this out, i didn't know this can happen \$\endgroup\$
    – dimitris93
    Jan 23, 2015 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .