二维三角形和矩形(AABB)之间的碰撞测试

发布于 2024-09-25 16:26:32 字数 373 浏览 8 评论 0原文

我花了很多时间让各种 2D 形状(圆-圆、圆-三、圆-矩形、矩形-矩形)之间的交集正确工作 - 非常感谢那些解决了这些问题的人,我从中得出了我的结论来自)一个简单项目的解决方案,现在正在尝试实施三角形-AABB 相交测试。

不过我有点卡住了。我尝试过在网上搜索并思考,但我无法得到任何想法。目前给我带来最大问题的是,当矩形内没有顶点时,检查三角形(顺便说一句,这是一个等腰三角形)的边是否与矩形相交。

我有什么想法可以让它发挥作用吗?

编辑:为了对我认为应该发生的阶段有更多的了解:

1 - 检查是否有任何顶点位于矩形中(这部分很简单)。如果是,则碰撞,否则继续。

2 - 检查是否有任何边与矩形相交。这就是我被困住的地方。我不知道如何实现这一点。

I've spent a good amount of time getting intersections working correctly between various 2D shapes (circle-circle, circle-tri, circle-rect, rect-rect - a huge thanks to those who've solved such problems from which I drew my solutions from) for a simple project and am now in the process of trying to implement an triangle-AABB intersection test.

I'm a bit stuck however. I've tried searching online and thinking it through however I've been unable to get any ideas. The thing that's given me the biggest issue at the moment is checking whether the edges of triangle (which is an isosceles btw) intersect the rectangle when no vertexes lie within the rectangle.

Any ideas how I could get this working?

EDIT: To give a bit more insight as to stages as I think they should occur:

1 - Check to see if any vertexes lie with in the rectangle (this part is easy). If yes, collision, otherwise continue.

2 - Check to see if any edges are intersecting the rectangle. This is where I'm stuck. I have little idea how to implement this.

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

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

发布评论

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

评论(1

笨死的猪 2024-10-02 16:26:32

我将计算定义矩形 4 条线的方程组,然后针对定义三角形线的方程组进行求解。

例如,给定一个具有最低点 (x1, y1) 且一侧的梯度为 g 的矩形,该矩形的其中一条线将为 y = gx + y1 >。找出方程来表示矩形的其他 3 条边。

形成三角形边的线将以类似方式计算。给定两点的直线方程为:

y - y1 = (x - x1) * (y2 - y1)/(x2 - x1)

如果存在任何可能的 x & y 值满足所有 7 个方程,那么就有一个交集。

编辑:我意识到虽然这是一个简单的算法,但编码起来可能很棘手;另一种选择是计算形成每条边(本质上是具有最小值和最大值的线)的间隔的公式并求解它们。

I'd calculate a collection of equations which define the 4 lines of the rectangle, and then solve against a collection of equations which define lines of the triangle.

For example, gievn a rectangle with lowest point (x1, y1) and one side having a gradient of g, one of the lines of the rectangle will be y = gx + y1. Find equations to represent the other 3 sides of the rectangle as well.

The lines which form the sides of the triangle will be calculated similarly. The equation for a line given two points is

y - y1 = (x - x1) * (y2 - y1)/(x2 - x1)

If there are any possible x & y values that satisy all 7 equations then you have an intersection.

edit: I realise that although this is a simple algorithm it might be tricky to code; another option is to calculate formulae for the intervals that form each edge (essentially lines with a min and max value) and solve these.

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