簡體   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