1

ピクセル パーフェクト コリジョンを下げましたが、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;
}

回転時の衝突に問題があります。回転したスプライトとのピクセルの衝突をチェックするにはどうすればよいですか? ありがとう、助けていただければ幸いです。

4

1 に答える 1

0

理想的には、行列を使用してすべての変換を表現できます。これは、ヘルパー メソッドの問題ではありません。また、行列を使用すると、衝突コードを変更せずにプログラムを簡単に拡張できます。これまでは、四角形の位置で平行移動を表すことができました。

オブジェクト A に変換がtransAあり、オブジェクト B に があるとしますtransB。次に、オブジェクト A のすべてのピクセルを反復処理します。ピクセルが透明でない場合は、オブジェクト B のどのピクセルがこの位置にあるかを確認し、このピクセルが透明かどうかを確認します。

トリッキーな部分は、オブジェクト B のどのピクセルが特定の位置にあるかを判断することです。これは、いくつかの行列演算で実現できます。

オブジェクト A の空間内の位置はわかっています。まず、このローカル位置を画面上のグローバル位置に変換します。これはまさに何をするかtransAです。その後、グローバル位置をオブジェクト B の空間内のローカル位置に変換します。これは の逆です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
    }
于 2013-09-30T12:10:24.527 に答える