1

スプライトを回転させるときにXNAを使用して作成しようとしている小惑星ゲームで、2D衝突検出に問題があります。衝突は、弾丸が小惑星に衝突する前に発生し続け(あらゆる側面から)、船が小惑星に衝突したときにも不正確になります。マトリックスを使用して船が撃った弾丸を回転させ、次に小惑星のマトリックスを使用し、XboxIndieGameフォーラムの1つでピクセルごとの衝突検出サンプルを使用しています。

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

私はプログラミングに不慣れで、衝突検出が不正確である理由を理解するためにここ数日を費やしました。マトリックスと、場合によってはピクセルごとの衝突検出方法が問題の原因である可能性があると思いましたが、何が問題なのかわかりません。助けてくれてありがとう!

4

1 に答える 1

0

ピクセル検出は、衝突におけるどちらのオブジェクトの回転も考慮していないようです。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

于 2013-02-26T11:45:46.637 に答える