Implementing per pixel collision on rotated sprites

有些话、适合烂在心里 提交于 2019-12-11 07:27:00

问题


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.


回答1:


I faced a very similar problem , when i was doing collision detection on a rotated sprited. What i basically did was rotate the rectangle bounding the sprite by an angle theta , here theta was the angle passed in the rotation parameter of spriteBatch.Draw()

A very naive implementation of what i am saying is.

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

If you do/did not understand what i am saying then please comment , i will try to elaborate.

Update: You should try doing this, I am assuming that the Second Sprite should be rotated, i haven't run this code

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


来源:https://stackoverflow.com/questions/14024248/implementing-per-pixel-collision-on-rotated-sprites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!