每像素碰撞变换比例旋转矩阵

时间:2014-04-18 01:12:39

标签: c# xna collision-detection xna-4.0 collision

我正在尝试使用缩放纹理2ds进行每像素碰撞。 它目前没有检测到碰撞,我不确定原因。

这是我创建矩阵转换的地方。

    private void BuildMatrix()
    {
        // Build the transformation matrix
        Matrix TransformMatrix =
            // _Rect.center.x and .y get the centers as this object is based off of a single square pixel.
            Matrix.CreateTranslation(new Vector3(-_Rect.Center.X, -_Rect.Center.Y, 0.0f)) *
            // _Width is the scale width and height is scale height.
            Matrix.CreateScale(_Width, _Height, 0) *
            // This one does not have rotation.
            Matrix.CreateRotationZ(0) *
            // Rect.X / Y are the top left X / Y coordinates for that rectangle.
            Matrix.CreateTranslation(new Vector3(_Rect.X, _Rect.Y, 0.0f));
    }

这是另一个构建矩阵。

    private void BuildMatrix()
    {
        // Build the transformation matrix
        Matrix TransformMatrix =
            // The location.center is the center of the texture 2d. Where it is placed on the screen.
            Matrix.CreateTranslation(new Vector3(-Location.Center, 0.0f)) *
            // Size width / height are the size of the texture after it is scaled.
            Matrix.CreateScale(Size.Width, Size.Height, 0) *
            // No rotation.
            Matrix.CreateRotationZ(0) *
            // Location.Position is the is the top coordinates for the texture2d before scaling.
            Matrix.CreateTranslation(new Vector3(Location.Position, 0.0f));
    }

这是每像素方法。

    public static bool IntersectPixels( Matrix transformA, int widthA, int heightA, Color[] dataA, Matrix transformB, int widthB, int heightB, Color[] dataB)
    {
        // Calculate a matrix which transforms from A's local space into 
        // world space and then into B's local space 
        Matrix transformAToB = transformA * Matrix.Invert(transformB);

        // When a point moves in A's local space, it moves in B's local space with a 
        // fixed direction and distance proportional to the movement in A. 
        // This algorithm steps through A one pixel at a time along A's X and Y axes 
        // Calculate the analogous steps in B: 
        Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
        Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

        // Calculate the top left corner of A in B's local space 
        // This variable will be reused to keep track of the start of each row 
        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 both pixels are not completely transparent, 
                    if (colorA.A != 0 && colorB.A != 0) { return true; } // then an intersection has been found 
                }
                // Move to the next pixel in the row 
                posInB += stepX;
            }
            // Move to the next row 
            yPosInB += stepY;
        }
        // No intersection found 
        return false;
    }

这是对每像素碰撞的调用。

    (Statistics.IntersectPixels(Projectile.TransformMatrix, Projectile.Txt2DImage.Width,
                                        Projectile.Txt2DImage.Height, Projectile.TextureColorArr,
                                        TextBoxContainer.Pillar.TransformMatrix, (int)TextBoxContainer.Pillar.Width,
                                        (int)TextBoxContainer.Pillar.Height, TextBoxContainer.Pillar.TextureColorArr)).ToString();

1 个答案:

答案 0 :(得分:0)

比例矩阵的Z值必须为1.

Matrix.CreateScale(_Width, _Height, 1)