2D中三角形和矩形之间的碰撞测试(AABB) [英] Collision test between a triangle and a rectangle (AABB) in 2D

查看:238
本文介绍了2D中三角形和矩形之间的碰撞测试(AABB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然而,我有点卡住了。我尝试过在线搜索并思考,但是我一直无法获得任何想法。现在给我的最大问题是检查三角形(这是一个isosceles btw)的边缘是否与长方形内没有顶点的矩形相交。



任何想法,我怎么能得到这个工作?



编辑:给我更多的阶段,因为我认为他们应该发生的见解:



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



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

解决方案

我计算了一组方程,它定义了矩形的4条线,然后解决定义三角形线的方程集。例如,gievn是一个具有最低点(x1,y1)的矩形,一边有一个 g 的梯度,矩形的其中一行将是 y = gx + y1 。找到方程来表示矩形的其他三条边。



构成三角形边的线将被类似地计算。给出两点的线的方程为:

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

如果有任何可能的x& y值满足所有7个等式,然后你有一个交点。

编辑:我意识到虽然这是一个简单的算法,但编码可能会很棘手;另一个选择是计算形成每条边的区间的公式(基本上是具有最小值和最大值的线)并解决这些问题。


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.

解决方案

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.

这篇关于2D中三角形和矩形之间的碰撞测试(AABB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆