繁体   English   中英

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

[英]Pixel-Perfect Collision With Texture Rotation

我的像素完美碰撞降低了,但是它仅适用于以0弧度旋转的纹理。 这是我用于确定像素完美碰撞的代码-

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

旋转碰撞时遇到麻烦。 如何检查旋转的精灵的像素碰撞? 谢谢,感谢您的帮助。

理想情况下,您可以使用矩阵排除所有转换。 助手方法应该不成问题。 而且,如果您使用矩阵,则可以轻松扩展程序,而无需更改冲突代码。 到目前为止,您可以使用矩形的位置来表示平移。

假设对象A具有转换transA ,对象B具有transB 然后,您将遍历对象A的所有像素。如果该像素不是透明的,请检查对象B的哪个像素在此位置,并检查该像素是否透明。

棘手的部分是确定对象B的哪个像素在给定位置。 这可以通过一些矩阵数学来实现。

您知道对象A的空间中的位置。首先,我们希望将此局部位置转换为屏幕上的全局位置。 这正是transA所做的。 之后,我们想将全局位置转换为对象B空间中的局部位置。这是transB的逆transB 因此,我们必须使用以下矩阵转换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