二维三角形和矩形(AABB)之间的碰撞测试
我花了很多时间让各种 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将计算定义矩形 4 条线的方程组,然后针对定义三角形线的方程组进行求解。
例如,给定一个具有最低点 (x1, y1) 且一侧的梯度为 g 的矩形,该矩形的其中一条线将为 y = gx + y1 >。找出方程来表示矩形的其他 3 条边。
形成三角形边的线将以类似方式计算。给定两点的直线方程为:
如果存在任何可能的 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 bey = 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
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.