Xna 设置精灵动画时的每像素碰撞
这是我用来检测碰撞的Xna 设置精灵动画时的每像素碰撞,xna,jquery-animate,pixel,Xna,Jquery Animate,Pixel,这是我用来检测碰撞的 public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB) { int top = Math.Max(rectangleA.Top, rectangleB.Top); int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bot
public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, 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);
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];
if (colorA.A != 0 && colorB.A != 0)
{
return true;
}
}
}
return false;
}:
现在我的猜测是,每次帧更改时,我都必须更新textureData
是否有方法仅从每个帧获取颜色数据?您可以获取完整的精灵片纹理数组,并且仅使用当前帧 假设您有一张精灵图纸,
stride
是一个像素到其下方像素的偏移量。这可以是精灵表的宽度。此外,您具有当前帧的第一个像素的位置x0
,y0
。然后您只需修改索引计算:
int posXInFrame = (x - rectangleA.Left);
int posYInFrame = (y - rectangleA.Top);
Color colorA = dataA[(posXInFrame + x0) + (posYInFrame + y0) * stride];
您可能已经在其他地方计算了
x0
和y0
,并可以将这些值传递给函数。关键在于获得正确的颜色[]数据,请向我们展示这些方法以及如何设置精灵的动画
int posXInFrame = (x - rectangleA.Left);
int posYInFrame = (y - rectangleA.Top);
Color colorA = dataA[(posXInFrame + x0) + (posYInFrame + y0) * stride];