【问题标题】:Collision Detection in XNA 4.0 InaccurateXNA 4.0 中的碰撞检测不准确
【发布时间】:2013-02-26 09:01:28
【问题描述】:

我在旋转精灵时尝试使用 XNA 制作的小行星游戏中的 2D 碰撞检测遇到了一些问题。在子弹击中小行星之前(似乎从各个方面),碰撞一直在发生,当船撞上小行星时也是不准确的。我使用矩阵旋转飞船发射的子弹,然后使用矩阵旋转小行星,并在 Xbox 独立游戏论坛之一上使用每像素碰撞检测示例。

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

这是我的子弹矩阵...原点和位置是子弹的 Vector2 属性

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

还有小行星……

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

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

【问题讨论】:

  • @AgentFire 我认为不需要定义。毕竟,OP 确实说过 The collision keeps happening before the bullet hits the asteroid (from every side it seems),这对我来说不够准确。
  • @Nolonar 好吧,之前多少钱?
  • 抱歉,我应该说得更具体一些,之前大概是 15 像素....有点难以分辨,但绝对是玩游戏的人会感到沮丧的明显数量。跨度>

标签: c# xna 2d collision-detection


【解决方案1】:

像素检测似乎没有考虑碰撞中任一对象的旋转。您应该多研究 Axis Aligned Bounding Boxes (AABB)。定向边界框会以与对象旋转相同的旋转角度旋转其碰撞相交,并且在您的情况下效果更好。

查看 GameDev 上的这篇文章,其中包含示例代码,可以带您走得更远:

https://gamedev.stackexchange.com/questions/15191/is-there-a-good-way-to-get-pixel-perfect-collision-detection-in-xna

【讨论】:

  • 非常感谢,我会试一试的!
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多