Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA 4.0中的碰撞检测不准确_C#_Xna_2d_Collision Detection - Fatal编程技术网

C# XNA 4.0中的碰撞检测不准确

C# XNA 4.0中的碰撞检测不准确,c#,xna,2d,collision-detection,C#,Xna,2d,Collision Detection,我在一个小行星游戏中遇到了一些2D碰撞检测的问题,我试图在旋转精灵时使用XNA。在子弹击中小行星之前(看起来是从四面八方),碰撞一直在发生,当飞船撞上小行星时,碰撞也是不准确的。我使用一个矩阵来旋转飞船射出的子弹,然后旋转一个小行星的矩阵,并且在一个Xbox Indie游戏论坛上使用每像素碰撞检测样本 public static bool IntersectPixels(Matrix transformA, int widthA, int heightA, Color[] dataA, Matr

我在一个小行星游戏中遇到了一些2D碰撞检测的问题,我试图在旋转精灵时使用XNA。在子弹击中小行星之前(看起来是从四面八方),碰撞一直在发生,当飞船撞上小行星时,碰撞也是不准确的。我使用一个矩阵来旋转飞船射出的子弹,然后旋转一个小行星的矩阵,并且在一个Xbox Indie游戏论坛上使用每像素碰撞检测样本

public static bool IntersectPixels(Matrix transformA, int widthA, int heightA, Color[] dataA, Matrix transformB, 
        int widthB, int heightB, Color[] dataB, float rotationA)
    {
        Matrix transformAToB = transformA * Matrix.Invert(transformB);

        Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
        Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

        Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);

        // For each row of pixels in A
        for (int yA = 0; yA < heightA; yA++)
        {
            // Start at the beginning of the row
            Vector2 posInB = yPosInB;

            // For each pixel in this row
            for (int xA = 0; xA < widthA; xA++)
            {
                // Round to the nearest pixel
                int xB = (int)Math.Round(posInB.X);
                int yB = (int)Math.Round(posInB.Y);

                // If the pixel lies within the bounds of B
                if (0 <= xB && xB < widthB &&
                    0 <= yB && yB < heightB)
                {
                    // Get the colors of the overlapping pixels
                    Color colorA = dataA[xA + yA * widthA];
                    Color colorB = dataB[xB + yB * widthB]

                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        return true;
                    }
                }
                // Move to the next pixel in the row
                posInB += stepX;
            }
            // Move to the next row
            yPosInB += stepY;
        }
        // No intersection found
        return false;
    }
还有小行星

        private void UpdateTransform()
    {
        Transform =
                Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
                Matrix.CreateTranslation(new Vector3(Position, 0.0f));
    }

我是编程新手,过去几天我一直在试图弄清楚为什么碰撞检测不准确。我认为矩阵和可能的每像素碰撞检测方法可能是造成问题的原因,但我不确定是什么错了。非常感谢您的帮助

像素检测似乎无法解释碰撞中任一对象的旋转。您应该进一步研究轴对齐边界框(AABB)。定向边界框将以与对象旋转相同的旋转度旋转其碰撞交点,并且在您的情况下效果更好

请看这篇关于GameDev的文章,其中包含示例代码,可以让您进一步了解:


@AgentFire我认为不需要定义。OP确实说过,在子弹击中小行星之前,碰撞一直在发生(似乎是从四面八方)毕竟,这对我来说已经足够不准确了。@Nolonar好吧,多少之前?对不起,我应该更具体一些,大概是15个像素之前……这有点难说,但是玩这个游戏的人肯定会感到沮丧。非常感谢你,我会试一试的!
        private void UpdateTransform()
    {
        Transform =
                Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
                Matrix.CreateTranslation(new Vector3(Position, 0.0f));
    }