Right now to do rotation I have a separate frame for each angle of rotation, but I much rather have one image that I rotate. My question is, how do I do a matrix transformation in XNA/C# so the color data is also rotated the same way the sprite is rendered on the screen? I found this old SO question, but I really don't understand the answer, and I'd like to know how the code works in case it needs to be modified later.
Any and all positional/rotational information needed is stored in the entity that contains the sprite, and both sprites are assumed to rotate.
Here is my code so far:
private static bool PixelPerfect(PhysicsEntity a, PhysicsEntity b)
{
if (!BoundingBox(a, b)) return false;
var colorDataA = new Color[a.Sprite.Source.Width * a.Sprite.Source.Height];
var colorDataB = new Color[b.Sprite.Source.Width * b.Sprite.Source.Height];
a.Sprite.Texture.GetData(0, a.Sprite.Source, colorDataA, 0, colorDataA.Length);
b.Sprite.Texture.GetData(0, b.Sprite.Source, colorDataB, 0, colorDataB.Length);
var top = (int) Math.Max(a.BoundingBox.Top, b.BoundingBox.Top);
var bottom = (int) Math.Min(a.BoundingBox.Bottom, b.BoundingBox.Bottom);
var left = (int) Math.Max(a.BoundingBox.Left, b.BoundingBox.Left);
var right = (int) Math.Min(a.BoundingBox.Right, b.BoundingBox.Right);
for (var y = top; y < bottom; y++)
{
for (var x = left; x < right; x++)
{
var colorA = colorDataA[(int) ((y - a.BoundingBox.Top) * (a.BoundingBox.Width) + (x - a.BoundingBox.Left))];
var colorB = colorDataB[(int) ((y - b.BoundingBox.Top) * (a.BoundingBox.Width) + (x - a.BoundingBox.Left))];
if (colorA.A == 0 || colorB.A == 0) continue;
a.CollisionPoint.ChangePositon(x, y);
b.CollisionPoint.ChangePositon(x, y);
return true;
}
}
return false;
}