Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

[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.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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!

Project: [color=#CC0000][font=Verdana, Helvetica, Tahoma, Arial]http://s000.tinyupload.com/?file_id=54830964798969346127[/font]

[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;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!