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.

4

1 回答 1

1

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

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

形成三角形边的线将被类似地计算。给定两点的直线方程是

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

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

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

于 2010-10-05T00:44:47.987 回答