简体   繁体   English

具有纹理旋转的完美像素碰撞

[英]Pixel-Perfect Collision With Texture Rotation

I have pixel-perfect collision down, but it only works with the texture rotated at 0 radians. 我的像素完美碰撞降低了,但是它仅适用于以0弧度旋转的纹理。 Here is my code for determining the pixel-perfect collision- 这是我用于确定像素完美碰撞的代码-

public static bool IntersectPixels(Texture2D sprite, Rectangle rectangleA, Color[] dataA, Texture2D sprite2, Rectangle rectangleB, Color[] dataB)
{
    sprite.GetData<Color>(dataA);
    sprite2.GetData<Color>(dataB);

    // Find the bounds of the rectangle intersection
    int top = Math.Max(rectangleA.Top, rectangleB.Top);
    int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
    int left = Math.Max(rectangleA.Left, rectangleB.Left);
    int right = Math.Min(rectangleA.Right, rectangleB.Right);

    // Check every point within the intersection bounds
    for (int y = top; y < bottom; y++)
    {
        for (int x = left; x < right; x++)
        {
            // Get the color of both pixels at this point
            Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
            Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];

            // If both pixels are not completely transparent,
            if (colorA.A != 0 && colorB.A != 0)
            {
                // then an intersection has been found
                return true;
            }
        }
    }

    // No intersection found
    return false;
}

I am having trouble with my collision when it is rotated. 旋转碰撞时遇到麻烦。 How would I go about checking pixel collision with a rotated sprite? 如何检查旋转的精灵的像素碰撞? Thanks, any help is appreciated. 谢谢,感谢您的帮助。

Ideally, you can exress all transformations with a matrix. 理想情况下,您可以使用矩阵排除所有转换。 That shouldn't be a problem with the helper methods. 助手方法应该不成问题。 And if you use matrices, you can easily extend the program without having to change the collision code. 而且,如果您使用矩阵,则可以轻松扩展程序,而无需更改冲突代码。 Up to now, you can represent a translation with the position of the rectangle. 到目前为止,您可以使用矩形的位置来表示平移。

Let's assume object A has the transformation transA and object B has transB . 假设对象A具有转换transA ,对象B具有transB Then you would iterate over all pixels of object A. If the pixel is not transparent, check which pixel of object B is at this very position and check if this pixel is transparent. 然后,您将遍历对象A的所有像素。如果该像素不是透明的,请检查对象B的哪个像素在此位置,并检查该像素是否透明。

The tricky part is determining which pixel of object B is at a given position. 棘手的部分是确定对象B的哪个像素在给定位置。 This can be achieved with some matrix math. 这可以通过一些矩阵数学来实现。

You know the position in the space of object A. Firstly, we want to transform this local position to a global position on the screen. 您知道对象A的空间中的位置。首先,我们希望将此局部位置转换为屏幕上的全局位置。 This is exactly what transA does. 这正是transA所做的。 After that, we want to transform the global location to a local location in the space of object B. This is the inverse of transB . 之后,我们想将全局位置转换为对象B空间中的局部位置。这是transB的逆transB So, we have to transform the local position in A with the following matrix: 因此,我们必须使用以下矩阵转换A中的局部位置:

var fromAToB = transA * Matrix.Invert(transB);
//now iterate over each pixel of A
for(int x = 0; x < ...; ++x)
    for(int y = 0; y < ...; ++y)
    {
        //if the pixel is not transparent, then
        //calculate the position in B
        var posInA = new Vector2(x, y);
        var posInB = Vector2.Transform(posInA, fromAToB);
        //round posInB.X and posInB.Y to integer values
        //check if the position is within the range of texture B
        //check if the pixel is transparent
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM