与矩形以外的形状碰撞..?

发布于 2024-10-03 00:57:01 字数 147 浏览 2 评论 0原文

我已经习惯了仅使用矩形进行碰撞检测,所以现在有点困惑。我正在处理类似钻石的形状,在过去的几个小时里,一直在试图找出如何检查碰撞。

我尝试检查第一个对象的四个点是否在第二个对象的点内,但这只是形成一个盒子(我认为)

我觉得我遇到困难的原因是因为角度。

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 技术交流群。

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

发布评论

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

评论(1

迟月 2024-10-10 00:57:01

您正在尝试将一个移动的凸多边形(您的“钻石”)与另一个移动的凸多边形碰撞,对吗?像这样的东西:

two moving Diamonds

你的第一步应该是将问题转换为一个等效的问题,其中一个多边形是静止的:

一个钻石以不同的速度移动,其他钻石静止

然后你可以将移动的多边形转换为“轴”覆盖移动多边形扫过的区域。这很简单:如果原始多边形有 n 条边,则轴有 n + 2 条边,额外的两条边与轴的长度和方向相同运动矢量。您可以通过根据与运动向量正交的分量对顶点进行排序,并在最大值处插入新边来找到插入这些新边的位置。

移动多边形转换为轴

现在您已将问题简化为静态多边形与静态多边形的问题。查看由 realtimerendering.com 提供的方便的碰撞算法表,并遵循参考资料,我们可以看到我们需要使用分离轴测试,例如本文,作者:David Eberly。

在二维中,如果我们能找到一个分离轴,即一条线,使得一个多边形落在该线的一侧,另一个多边形落在另一侧,则两个凸多边形就无法相交:

两个凸多边形和一个将它们分开的轴

如果给定一个方向,我们可以很容易地发现是否存在一条在通过将两个多边形投影到垂直于该方向的直线上,并查看投影是否不相交:

我们如何知道分离轴将走向哪个方向?好吧,如果存在任何分离轴,那么就有一个与凸多边形之一的一侧平行的分离轴(请参见Eberly,第 3 页)。因此,只有一小部分方向需要检查,如果您检查了所有方向而没有找到分离轴,则两个多边形相交(因此原始移动对象会发生碰撞)。

您可以进行很多改进和优化,当然不限于这些:

  1. 在进行完整的移动多边形/多边形测试之前,进行一个更简单的测试,例如圆/圆,以便您可以快速拒绝简单的情况。
  2. 使用某种空间分区方案(例如四叉树),以便仅测试距离足够近以至于可能发生碰撞的对象。
  3. “缓存目击者”——如果一条线在时间 t 时分隔两个对象,则它很可能会在时间 t + δ 时继续分隔它们,因此记住这条线是值得的您找到的分离轴,下次先尝试一下(请参阅 Rabbitz,“Graphics Gems IV 中的“nofollow noreferrer">移动凸多面体的快速碰撞检测”)。

但不要太担心优化:先把它做好,然后才能加快速度。

You're trying to collide a moving convex polygon (your "diamond") with another moving convex polygon, is that right? Something like this:

two moving diamonds

Your first step should be to transform the problem to an equivalent one in which one of the polygons is stationary:

one diamond moving with difference of velocities, other diamond 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.

the moving polygon transformed to a shaft

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:

two convex polygons and an axis that separates them

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:

two convex polygons projected onto a line are disjoint, showing the existence of a separating axis

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:

  1. Before doing the full moving polygon/polygon test, do a simpler test like circle/circle so that you can reject easy cases quickly.
  2. Use some kind of spatial partitioning scheme like quadtrees so that you only test objects that are close enough that they might collide.
  3. "Caching witnesses" — if a line separates two objects at time t, it's likely that it continues to separate them at time t + δ, so it can pay to remember the separating axis you found and try it first next time (see Rabbitz, "Fast Collision Detection of Moving Convex Polyhedra" in Graphics Gems IV).

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.

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