【发布时间】:2016-06-06 16:15:27
【问题描述】:
现在要进行旋转,每个旋转角度都有一个单独的框架,但我更愿意旋转一个图像。我的问题是,如何在 XNA/C# 中进行矩阵转换,以便颜色数据也以与精灵在屏幕上呈现相同的方式旋转?我找到了这个老SO question,但是我真的不明白答案,我想知道代码是如何工作的,以防以后需要修改。
所需的任何和所有位置/旋转信息都存储在包含精灵的实体中,并且假设两个精灵都旋转。
到目前为止,这是我的代码:
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;
}
【问题讨论】:
标签: c# xna collision-detection