0

WindowsForm に 2 つの四角形があり、それらが衝突するかどうかを確認したいと思います。単純な非回転衝突の場合、次のようになります。

Point newLocation; // upper-left corner of the object to check its collision
Size objectSize; // the object size
bool collision = false;

foreach (Object otherObject in otherObjects)
{
    if (newLocation.X >= otherObject.location.X && newLocation.X <= otherObject.location.X + otherObject.size.width)
        if (newLocation.Y >= otherObject.location.Y && newLocation.Y <= otherObject.location.Y + otherObject.size.height)
        {
            collision = true;
            break;
        }
}

しかし今、私は両方のオブジェクトを次のように回転させました:

Matrix matrix = new Matrix();
matrix.RotateAt(angle, newLocation);
graphics.Transform = matrix;

回転した行列で衝突を確認するにはどうすればよいですか? 翻訳された X、Y 座標を取得できますか?

4

1 に答える 1

0

標準座標系から特定の座標系にポイントを転送するコードがいくつかあります (ただし、Y は画面内で下方に増加するため、いくつかの調整が行われ、コメントが付けられました)。

ここで、double[] は点を表し、インデックス 0 は X 座標、インデックス 1 は Y です。新しい座標系の角度は反時計回りにラジアン単位で測定されることに注意してください。(度をラジアンに変換するには、Pi/180 を掛けます)。

    /// <summary>
    /// Implemented - Returns the point coordinates related to a new coordinate system
    /// Does not change original point
    /// </summary>
    /// <param name="Point">Point to be returned in new coordinate system</param>
    /// <param name="NewSystemCouterClockRotation">CounterClokWise Angle of rotation of the new coordinate system compared to the current, measured in radians</param>
    /// <param name="NewSystemOrigin">Location of the new origin point in the current coordinate system</param>
    /// <returns></returns>
    public double[] ChangeCoordinateSystem(double[] Point, double NewSystemCouterClockRotation, double[] NewSystemOrigin)
    {

        //first adjust: fix that winform downwards increasing Y before applying the method
        Point[1] = -Point[1];
        NewSystemOrigin[1] = -NewSystemOrigin[1]

        //end of first adjust


        //original method
        double[] Displacement = new double[2] { Point[0] - NewSystemOrigin[0], Point[1] - NewSystemOrigin[1] };


        double[] Result = new double[2]
        {
            + Displacement[0] * Math.Cos(NewSystemCouterClockRotation) + Displacement[1] * Math.Sin(NewSystemCouterClockRotation), 
            - Displacement[0] * Math.Sin(NewSystemCouterClockRotation) + Displacement[1] * Math.Cos(NewSystemCouterClockRotation) 
        }; 

        //second adjust: reset Y of the result
        Result[1] = - Result[1];

        return Result;
    }

ただし、2 つのオブジェクトの角度が異なる場合は注意が必要です。これを行う最善の方法all four corners of the firstは、最初の長方形が他のオブジェクトの内側にないかどうか、および最初の長方形の内側にもないかどうかを確認することthe other object four cornersです。

ポイントがポリゴンの内側にあるかどうかを調べるアルゴリズムは、次の場所にあります: ポリゴン内の ポイント

于 2013-05-24T18:31:33.923 に答える