1

我正在使用 C# 在 XNA 中构建一个 2D 游戏,并且我正在使用精灵来跟踪玩家的位置并在 spritebatch.Draw() 方法中相应地旋转。我现在正在尝试实现逐像素碰撞检测,并且我相信精灵的旋转会将其扔掉。碰撞检查如下。

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

如果有帮助的话,只有一组被检查的精灵会被旋转。

4

1 回答 1

0

当我对旋转的精灵进行碰撞检测时,我遇到了一个非常相似的问题。我基本上所做的是将与精灵边界的矩形旋转角度 theta ,这里 theta 是 spriteBatch.Draw() 的旋转参数中传递的角度

我所说的是一个非常天真的实现。

for each X in RectangleofSpriteToBeRotated{
   for each Y in RectangleofSpriteToBeRotated{

    Vector2 temp = Vector2.Transform(PointXY, Matrix.CreateRotationZ(Theta));
     //Save Point Temp in a new 2D Array 
    }
}

如果您确实/不明白我在说什么,请发表评论,我会尽力详细说明。

更新:您应该尝试这样做,我假设应该旋转第二个 Sprite,我还没有运行此代码

private bool collision(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
    {

    var  RotatedP0=  new Vector2.Transform( new Vector2( object2.Top,object2.Left ),Matrix.CreateRotationZ(theta));
    var  RotatedP1=  new Vector2.Transform( new Vector2 (object2.Bottom,object2.Right),Matrix.CreateRotationZ(theta ) );    


        if (object1.Bottom < RotatedP0.Y)
            return perPixel(object1, dataA, object2, dataB);
       if (object1.Top > RotatedP1.Y)
           return perPixel(object1, dataA, object2, dataB);
       if (object1.Left > RotatedP1.X)
           return perPixel(object1, dataA, object2, dataB);
       if (object1.Right < RotatedP0.X)
           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];

                Vector2 v = Vector2.Transform(new Vector2(x,y), Matrix.CreateRotationZ(theta));

                Color colourB = dataB[ (int) v.X, (int) v.Y];

                if (colourA.A != 0 && colourB.A != 0)
                {
                    return true;
                }
            }
        }
        return false;
    }
于 2012-12-24T18:06:33.253 回答