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

发布于 2024-12-22 12:19:05 字数 184 浏览 2 评论 0原文

我在谷歌上找不到创建 2D 碰撞 AABB 的方法,我想知道它的数学原理是如何工作的。我当时想我可以使用某种矩阵变换来实现它,但这个想法彻底失败了。所以基本上,我想知道任何可以帮助我创建角度对齐边界框并检查点是否与其相交的资源,或者对其数学和逻辑的解释。

编辑:我不知道我是否说清楚了,但我需要测试与它们的碰撞。只是为了让这一点变得清晰。

I've found no way of creating a 2D collision AABB on google, and I was wondering how the maths of it work. I was thinking that I could use some kind of matrix transforms to achieve it but that idea failed epically. So basically, I want to know of any resource that can help me create an Angle-Aligned Bounding Box and check if a point intersects it, or an explanation of the maths and logic of it.

Edit: I don't know if I made it clear, but I need to test collisions with them. Just to make that crystal clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-12-29 12:19:05

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

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

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。

An intersection check between two Rectangles does not allow you to check against a rotated rectangle. The as the simple intersects function will not work. Your idea of calculating a Rectangle that encompasses your "actual" rotated Rectangle, and then checking for collisions on the Bounding Rectangles instead.

Here is some code that will return a Rectangle based on another Rectangle and a Rotation Matrix transformation:

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

Of course, this type of collision detection is fast, but not accurate. Your Bounding rectangle will not be a 1:1 representation of your shape. If you need more accuracy, you could then use a per-pixel collision check after checking with AABB.

This answer might also be of interest to you, particularly the answer on SAT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文