2

I'm currently trying to understand per pixel collision detection.

This is the code I don't understand:

static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                            Rectangle rectangleB, Color[] dataB)
{
    // Find the bounds of the rectangle intersection
    int top = Math.Max(rectangleA.Top, rectangleB.Top);
    int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
    int left = Math.Max(rectangleA.Left, rectangleB.Left);
    int right = Math.Min(rectangleA.Right, rectangleB.Right);

    // Check every point within the intersection bounds
    for (int y = top; y < bottom; y++)
    {
        for (int x = left; x < right; x++)
        {
            // Get the color of both pixels at this point
            Color colorA = dataA[(x - rectangleA.Left) +
                                 (y - rectangleA.Top) * rectangleA.Width];
            Color colorB = dataB[(x - rectangleB.Left) +
                                 (y - rectangleB.Top) * rectangleB.Width];

            // If both pixels are not completely transparent,
            if (colorA.A != 0 && colorB.A != 0)
            {
                // then an intersection has been found
                return true;
            }
        }
    }

    // No intersection found
    return false;
}

I really haven't understood the all loop. I'll be glad for some explanation how it works.

dtb
  • 213,145
  • 36
  • 401
  • 431
Joe
  • 41
  • 2
  • 6

3 Answers3

7

First up, it finds the region the two image rectangles intersect, then it iterates through each pixel in that region, and compares the alpha values of each image of each pixel. If neither has an alpha value of 0, they are both considered 'solid' and therefore colliding.

enter image description here

Darcy Rayner
  • 3,385
  • 1
  • 23
  • 15
5

it's not that hard (in this case) - you give the algorithm the two bounding-boxes of your objects (so the hole object is inside this box), and a array with color-information for them. Tha algorithm assumes that a point belongs to the object IFF it is not transparent - this is important.

The first step is to calculate the intersecting rectangle - if you intersect two rectangles that have sides parallel to the axes like in this case - you will get a rectangle again or an empty set.

The next step is to iterate in this intersecting rectangle for all (x,y) -coordinates insiede - first y, then x -so you get your normal first x, then y inside, but this is minor point and not important.

Then finally the algorithm gets the color for object A and B at the current pixel (x,y) - if both colors are NOT transparent then the pixel is in both objects and the objects have to intersect at this point - so the algorithm terminates with "YES they intersect"

If all pixels in the intersection of the bounding boxes where checked and no common (e.g. not transparent) pixel was found the object don't intersect and so the algorithm terminates with "NO they don't intersect"

I hope this helps.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Your explanation helped me alot, but one thing I haven't understood is, in the loop, how he really finds the current pixel color, like I mean, why I need to multiply the rectangle Width in the end of it? – Joe Sep 03 '11 at 11:54
  • And another question please: Is this algorithm finds the per pixel only in the intersection bounds and not in the whole both rectangles right? – Joe Sep 03 '11 at 11:58
  • second comment: yes thats right, first comment: that's the part were the array-operations with dataA and dataB are done (first two things in inner loop) - inside is just a bit of coordinate-transform from "world"-view into "object"-view (the top/left point in the object has for example coordinates RectangleA.top/RectangleA.left in worldview, but of course coordinates 0/0 for the color-information of the object) – Random Dev Sep 03 '11 at 12:06
  • So you mean he looks only in the red bounds, like this? http://img7.imageshack.us/img7/4913/dfsb.png – Joe Sep 03 '11 at 12:16
  • @CarstenKönig I'm totally new to this collision detection, I get everything except the bit about the 2nd and 4th parameters (array of colours). Where does this come from? – imperium2335 Mar 29 '15 at 11:51
0

for (int y = top; y < bottom; y++) loops over the lines of the resulting rectangle from top to bottom, and for (int x = left; x < right; x++) loops over pixels inside each line, left to right.

www0z0k
  • 4,444
  • 3
  • 27
  • 32