Public Group

# [C# XNA] Pixel Collision

This topic is 2957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

The method below is from my project that uses pixel collision. The collision "works", but I'm encountering a bug during the test. When the collision is detected on the right and bottom side of Texture1, the pixel collision works perfectly. However when the collision is detected on the left and top side of Texture1, Texture2 has to intersect with half it's size before collision is detected. I've added the entire project if you would like to take a look and below is the collision method. Please help!

[source lang="cs"]

public bool IntersectPixels(
Matrix transformA, Rectangle rectangleA, int widthA, int heightA, Color[] dataA,
Matrix transformB, Rectangle rectangleB, int widthB, int heightB, Color[] dataB)
{
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);

rectangle = new Rectangle(left, top, right - left, bottom - top);

// Calculate a matrix which transforms from A's local space into
// world space and then into B's local space
Matrix transformAToB = transformA * Matrix.Invert(transformB);

// When a point moves in A's local space, it moves in B's local space with a
// fixed direction and distance proportional to the movement in A.
// This algorithm steps through A one pixel at a time along A's X and Y axes
// Calculate the analogous steps in B:
Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

// Calculate the top left corner of A in B's local space
// This variable will be reused to keep track of the start of each row
Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);

// For each row of pixels in A
for (int yA = 0; yA < rectangle.Height; yA++)
{
// Start at the beginning of the row
Vector2 posInB = yPosInB;

// For each pixel in this row
for (int xA = 0; xA < rectangle.Height; xA++)
{
// Round to the nearest pixel
int xB = (int)Math.Round(posInB.X);
int yB = (int)Math.Round(posInB.Y);

// If the pixel lies within the bounds of B
if (0 <= xB && xB < widthB &&
0 <= yB && yB < heightB)
{

if (0 <= xA && xA < widthA &&
0 <= yA && yA < heightA)
{
// Get the colors of the overlapping pixels
Color colorA = dataA[xA + yA * widthA];
Color colorB = dataB[xB + yB * widthB];

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

// Move to the next pixel in the row
posInB += stepX;
}

// Move to the next row
yPosInB += stepY;
}

// No intersection found
return false;
}
[/source]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11