与矩形以外的形状碰撞..?
我已经习惯了仅使用矩形进行碰撞检测,所以现在有点困惑。我正在处理类似钻石的形状,在过去的几个小时里,一直在试图找出如何检查碰撞。
我尝试检查第一个对象的四个点是否在第二个对象的点内,但这只是形成一个盒子(我认为)
我觉得我遇到困难的原因是因为角度。
I'm so used to working just with rectangles for collision detection that I'm a bit stumped right now. I'm working with diamond-like shapes, and for the past few hours, have been trying to figure out how to check for collision.
I tried checking to see if the first objects four points are inside the points of the second object, but that just makes a box (I think)
The reason why I feel like I'm having difficulty with this is because of the angles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将一个移动的凸多边形(您的“钻石”)与另一个移动的凸多边形碰撞,对吗?像这样的东西:
你的第一步应该是将问题转换为一个等效的问题,其中一个多边形是静止的:
然后你可以将移动的多边形转换为“轴”覆盖移动多边形扫过的区域。这很简单:如果原始多边形有 n 条边,则轴有 n + 2 条边,额外的两条边与轴的长度和方向相同运动矢量。您可以通过根据与运动向量正交的分量对顶点进行排序,并在最大值处插入新边来找到插入这些新边的位置。
现在您已将问题简化为静态多边形与静态多边形的问题。查看由 realtimerendering.com 提供的方便的碰撞算法表,并遵循参考资料,我们可以看到我们需要使用分离轴测试,例如本文,作者:David Eberly。
在二维中,如果我们能找到一个分离轴,即一条线,使得一个多边形落在该线的一侧,另一个多边形落在另一侧,则两个凸多边形就无法相交:
如果给定一个方向,我们可以很容易地发现是否存在一条在通过将两个多边形投影到垂直于该方向的直线上,并查看投影是否不相交:
我们如何知道分离轴将走向哪个方向?好吧,如果存在任何分离轴,那么就有一个与凸多边形之一的一侧平行的分离轴(请参见Eberly,第 3 页)。因此,只有一小部分方向需要检查,如果您检查了所有方向而没有找到分离轴,则两个多边形相交(因此原始移动对象会发生碰撞)。
您可以进行很多改进和优化,当然不限于这些:
但不要太担心优化:先把它做好,然后才能加快速度。
You're trying to collide a moving convex polygon (your "diamond") with another moving convex polygon, is that right? Something like this:
Your first step should be to transform the problem to an equivalent one in which one of the polygons is stationary:
Then you can transform the moving polygon into a "shaft" that covers the area swept by the moving polygon. This is straightforward to do: if the original polygon has n sides, then the shaft has n + 2 sides, with the extra two sides being the same length and direction as the movement vector. You find where to insert these new sides by sorting the vertices according to their component that's orthogonal to the movement vector, and inserting new sides at the maxima.
Now you've reduced the problem to static polygon against static polygon. Taking a look at the handy table of collision algorithms courtesy of realtimerendering.com, and following the references, we can see that we need to use the separating axis test, for example as described in section 3 of this paper by David Eberly.
In two dimensions, two convex polygons fail to intersect if we can find a separating axis, a line such that one polygon falls on one side of the line, and the other polygon on the other:
If we are given a direction, we can easily discover if there exists a separating axis that runs in that direction, by projecting the two polygons onto a line running perpendicular to that direction, and looking to see whether the projections are disjoint:
How do we know which direction the separating axis will run in? Well, if any separating axis exists, then there's one that runs parallel to one of the sides of one of the convex polygons (see Eberly, page 3). So there's only a small set of directions to check, and if you've checked them all without finding a separating axis, then the two polygons intersect (and hence the original moving objects collide).
There are lots of refinements and optimizations you can make, certainly not limited to these:
But don't worry too much about optimizing: get it right first in the confidence that you'll be able to speed it up later.