0

I tried to understand the code that checks the collision between two objects in the game - per pixel collision. The code is:

/// <summary>
/// Determines if there is overlap of the non-transparent pixels
/// between two sprites.
/// </summary>
/// <param name="rectangleA">Bounding rectangle of the first sprite</param>
/// <param name="dataA">Pixel data of the first sprite</param>
/// <param name="rectangleB">Bouding rectangle of the second sprite</param>
/// <param name="dataB">Pixel data of the second sprite</param>
/// <returns>True if non-transparent pixels overlap; false otherwise</returns>
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 found some explanations but no one explained the last lines:

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

what is this colorA.A property? why this if-statement determine that a collision occured?

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Michael
  • 427
  • 1
  • 3
  • 13
  • If both pixels are non-transparent, then two pixels are colliding. Ie ignore transparent pixels. 'A' is the alpha value – axon May 30 '13 at 09:40

1 Answers1

4

The Color.A property is the alpha-value of the color. 0 means completely transparent, and 255 means completely opaque.

If one of the colors is completely transparent at the specified pixel, there is no collision (because one of the objects is not at this place, but only its bounding box).

Only if both of them are != 0, there actually are two objects at the same place and a collision has to be handled.

See for example this image: bb intersection

The bounding boxes (black rectangles) intersect, but you would not consider it to be a collision between the red and yellow circle.

Therefore, you have to check the color at the intersecting pixels. If they are transparent (white in this example), the circles themselves are not intersecting.

This is why you have to check if the colors of the objects are transparent. If one of them is transparent, the object itself is not intersecting the other object, only its bounding box.

pascalhein
  • 5,700
  • 4
  • 31
  • 44
  • i am sorry, but i dont understand what transparency has to do with the collision checking. Maybe i dont understand the whole code, not juse these lines. – Michael May 30 '13 at 11:46