I am building a 2D game in XNA using C#, and I am using sprites that will track the player's position and rotate accordingly in the spritebatch.Draw() method. I am now trying to implement per-pixel collision detection, and I believe that the rotation of the sprites is throwing it off. The collision checks are as follows.
private bool collision(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
{
if (object1.Bottom < object2.Top)
return perPixel(object1, dataA, object2, dataB);
if (object1.Top > object2.Bottom)
return perPixel(object1, dataA, object2, dataB);
if (object1.Left > object2.Right)
return perPixel(object1, dataA, object2, dataB);
if (object1.Right < object2.Left)
return perPixel(object1, dataA, object2, dataB);
return true;
}
private bool perPixel(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
{
//Bounds of collision
int top = Math.Max(object1.Top, object2.Top);
int bottom = Math.Min(object1.Bottom, object2.Bottom);
int left = Math.Max(object1.Left, object2.Left);
int right = Math.Min(object1.Right, object2.Right);
//Check every pixel
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
Color colourA = dataA[x, y];
Color colourB = dataB[x, y];
if (colourA.A != 0 && colourB.A != 0)
{
return true;
}
}
}
return false;
}
Only one of the sets of sprites being checked against will be rotated, if that will help.