C# 如何检查自定义2D物理引擎的点是否与AABB相交

C# 如何检查自定义2D物理引擎的点是否与AABB相交,c#,math,xna,collision-detection,physics,C#,Math,Xna,Collision Detection,Physics,我在谷歌上找不到创建二维碰撞AABB的方法,我想知道它的数学原理。我想我可以用某种矩阵变换来实现它,但这个想法失败了。因此,基本上,我想知道任何资源,可以帮助我创建一个角度对齐的边界框,并检查一个点是否与它相交,或者对它的数学和逻辑进行解释 编辑:我不知道我是否说得很清楚,但我需要测试与它们的碰撞。这一点非常清楚。两个矩形之间的相交检查不允许您对照旋转的矩形进行检查。因为简单的相交函数将不起作用。您的想法是计算一个包含“实际”旋转矩形的矩形,然后检查边界矩形上的碰撞 下面是一些基于另一个矩形和旋

我在谷歌上找不到创建二维碰撞AABB的方法,我想知道它的数学原理。我想我可以用某种矩阵变换来实现它,但这个想法失败了。因此,基本上,我想知道任何资源,可以帮助我创建一个角度对齐的边界框,并检查一个点是否与它相交,或者对它的数学和逻辑进行解释


编辑:我不知道我是否说得很清楚,但我需要测试与它们的碰撞。这一点非常清楚。

两个矩形之间的相交检查不允许您对照旋转的矩形进行检查。因为简单的相交函数将不起作用。您的想法是计算一个包含“实际”旋转矩形的矩形,然后检查边界矩形上的碰撞

下面是一些基于另一个矩形和旋转矩阵变换返回矩形的代码:

public static Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform)
{
    // Get all four corners in local space
    Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);
    Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);
    Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);
    Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);

    // Transform all four corners into work space
    Vector2.Transform(ref leftTop, ref transform, out leftTop);
    Vector2.Transform(ref rightTop, ref transform, out rightTop);
    Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
    Vector2.Transform(ref rightBottom, ref transform, out rightBottom);

    // Find the minimum and maximum extents of the rectangle in world space
    Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
                                Vector2.Min(leftBottom, rightBottom));
    Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
                                Vector2.Max(leftBottom, rightBottom));

    // Return that as a rectangle
    return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
}
当然,这种类型的碰撞检测速度很快,但并不准确。您的边框将不是形状的1:1表示形式。如果需要更高的精度,可以在使用AABB检查后使用每像素碰撞检查


答案可能也会引起您的兴趣,尤其是SAT上的答案。

两个矩形之间的相交检查不允许您对照旋转的矩形进行检查。因为简单的相交函数将不起作用。您的想法是计算一个包含“实际”旋转矩形的矩形,然后检查边界矩形上的碰撞

下面是一些基于另一个矩形和旋转矩阵变换返回矩形的代码:

public static Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform)
{
    // Get all four corners in local space
    Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);
    Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);
    Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);
    Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);

    // Transform all four corners into work space
    Vector2.Transform(ref leftTop, ref transform, out leftTop);
    Vector2.Transform(ref rightTop, ref transform, out rightTop);
    Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
    Vector2.Transform(ref rightBottom, ref transform, out rightBottom);

    // Find the minimum and maximum extents of the rectangle in world space
    Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
                                Vector2.Min(leftBottom, rightBottom));
    Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
                                Vector2.Max(leftBottom, rightBottom));

    // Return that as a rectangle
    return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
}
当然,这种类型的碰撞检测速度很快,但并不准确。您的边框将不是形状的1:1表示形式。如果需要更高的精度,可以在使用AABB检查后使用每像素碰撞检查


答案可能也会引起您的兴趣,尤其是SAT上的答案。

我现在提出了这个问题:但我不知道如何让它起作用。您想让AABB做什么?它是一个简单的长方形或圆形,还是一个复杂的精灵?@David我只需要为我的物理引擎做一个。当我制作游戏时,它应该包含我想要的任何东西。但现在我将保持旋转的矩形,这是非常模糊的。为了回答你的问题,我们需要更多的细节。你能不能举一个简短的例子,说明你拥有的物品种类和你迄今为止尝试过的东西?@DavidBrown事实上我不需要细节,只需要它们如何工作以及如何找到一个点是否与AABB相交的逻辑。我现在已经移植了这个:但我不知道如何让它工作。你想让AABB围绕什么?它是一个简单的长方形或圆形,还是一个复杂的精灵?@David我只需要为我的物理引擎做一个。当我制作游戏时,它应该包含我想要的任何东西。但现在我将保持旋转的矩形,这是非常模糊的。为了回答你的问题,我们需要更多的细节。你能不能举一个简短的例子,说明你拥有的对象类型和你迄今为止尝试过的东西?@DavidBrown事实上我不需要细节,只需要它们工作原理背后的逻辑以及如何找到一个点是否与aabbWell相交事实上我更感兴趣的是检查旋转矩形和一个点之间的碰撞,没有计算旋转矩形周围的新边界框,但无论如何谢谢。实际上我更感兴趣的是检查旋转矩形和点之间的碰撞,没有计算旋转矩形周围的新边界框,但无论如何谢谢。