简体   繁体   English

如何检查旋转对象的平移点碰撞?

[英]How to check for rotated objects translated points collisions?

I've got two rectangles on WindowsForm and I would like to check if they collide. 我在WindowsForm上有两个矩形,我想检查它们是否碰撞。 For simple non-rotated collision it looks like this: 对于简单的非旋转碰撞,它看起来像这样:

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

But now I rotated both objects with: 但是现在我旋转了两个对象:

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

How can I check for the collisions at the rotated matrix? 如何检查旋转矩阵处的碰撞? Can I somehow get the translated X, Y coordinates? 我能以某种方式获得平移的X,Y坐标吗?

I have some code to transfer points from the standard coordinate system to a specific coordinate system (but in you case, Y increases downards in screen, so some adjusts were made and commented). 我有一些代码可以将点从标准坐标系转移到特定坐标系(但是在您的情况下,Y增加了屏幕上的颜色,因此进行了一些调整和注释)。

Here, the double[] represents a point, where index 0 is X coordinate and index 1 is Y. Notice the angle of the new coordinate system is measurede counterclockwise and in radians. 在这里,double []代表一个点,其中索引0为X坐标,索引1为Y。请注意,新坐标系的角度是逆时针并以弧度为单位。 (Multiply by Pi/180 to transform degrees to radians). (乘以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;
    }

But, if your two objects have different angles, you should be careful, the best way to do that is to check if all four corners of the first of the first rectangle are not inside the other object AND if the other object four corners are not inside the first as well. 但是,如果您的两个对象的角度不同,则应当心,最好的方法是检查all four corners of the first一个矩形中第一个矩形的all four corners of the first是否不在另一个对象内, the other object four corners是否不在the other object four corners内里面也一样。

Some algorythm to find out if a point is inside a polygon can be found here: Point in polygon 可以在此处找到一些算法来找出点是否在多边形内: 点在多边形中

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

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