对平台游戏主角/背景碰撞解决的看法

发布于 2024-09-05 22:42:07 字数 390 浏览 2 评论 0原文

想象一下以下场景:我有一个关卡,其物理结构是由边界矩形的集合与预渲染的位图背景相结合构建的。我的演员,包括玩家角色,都有自己的边界矩形。 如果一个角色设法卡在一个关卡块内,无论是部分还是其他,它都需要再次移出,以便它与该块齐平。

我在生物休息期间想到的未经测试的技术如下:

如果发现演员的盒子与水平盒子相交,请确定每个矩形的中心点在哪里。如果角色的中心高于关卡框的中心,则移动角色,使角色矩形的底部与关卡矩形的顶部齐平,如果较低,则反之亦然。然后水平做类似的事情。

对此有何意见?关于更好方法的建议?

实际上,边界矩形是 XNA BoundingBox,其 Z 范围从 -1 到 1,但它仍然是 2D 游戏。

Imagine the following scenario: I have a level whose physical structure is built up from a collection of bounding rectangles, combined with prerendered bitmap backgrounds. My actors, including the player character, all have their own bounding rectangle. If an actor manages to get stuck inside a level block, partially or otherwise, it'll need to be shifted out again, so that it is flush against the block.

The untested technique I thought up during bio break is as follows:

If an actor's box is found to intersect a level box, determine where the centerpoints of each rect are. If the actor's center is higher than the level box's, move the actor so that the bottom of the actor's rect is flush with the top of the level's rect, and vice versa if it's lower. Then do a similar thing horizontally.

Opinions on that? Suggestions on better methods?

Actually, the bounding rects are XNA BoundingBoxes with their Z spanning from -1 to 1, but it's still 2D gameplay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

花海 2024-09-12 22:42:07

您读过N 教程吗?它们是关于基于碰撞检测和简单的分离轴定理的精彩介绍,并配有一些小演示投影响应。 (它们实际上在 N 游戏 中使用,效果非常好。)教程涵盖的内容比您要多需要,但它们非常通用(可扩展到许多其他形状),并开始涉及快速移动物体和其他响应技术的问题。

即使您确实决定采用比 SAT 实施更简单的方法,这也可能会给您带来很多好主意。

(完成后,如果您想让自己大吃一惊,请尝试查看游戏开发者大会物理教程日的一些演示文稿,包括 realtimecollisiondetection.net 出版物essentialmath.com 教程幻灯片,这两本书,和/或从这些网站链接的其他内容,我也强烈推荐 GDC 会议本身,虽然我们仍然不需要或特别想要在手持游戏平台上的扫掠船体上使用花哨的 GJK。工作中,一些更简单的概念,例如“配置空间”和 Minkowski 和与差极大地影响了我对物理和碰撞检测的看法以及我们如何实现它。)

Have you read the N Tutorials? They're a wonderful introduction, complete with little demos, of Separating Axis Theorem based collision detection and simple projection response. (They're actually used in the N game to great effect.) The tutorials cover more than you need, but they're very general (extensible to many other shapes), and start to touch on issues with fast-moving objects and other response techniques.

Even if you do decide to go with something simpler than a SAT implementation, this may give you a lot of good ideas.

(When you're done with that and if you want your mind blown, try looking into some of the presentations from the Game Developers Conference physics tutorial day, including realtimecollisiondetection.net publications, the essentialmath.com tutorial slides, both of those books, and/or other stuff linked from those sites. I'd highly recommend the GDC session itself, too. While we still don't need or particularly want fancy GJK on swept hulls on the handheld game platforms we work with, some of the simpler concepts such as "configuration spaces" and Minkowski sums and differences have greatly influenced how I think about physics and collision detection and how we implement it.)

笑咖 2024-09-12 22:42:07

“真正的”物理引擎所做的是找到最小穿透向量。也就是说,代表两个物体相互渗透多深的最小向量。

对于 AABB(轴对齐边界框)来说,这非常容易计算。

(考虑制作自己的 2D AABB 结构,它会更小,因此性能更好。)

一旦获得最小穿透矢量,就可以执行碰撞响应。最简单的响应是简单地通过该向量将两个对象分开(或者如果另一个对象是静态的,则将一个对象分开)。

这是 N 的制作者提供的一个很好的参考,介绍如何对凸函数执行此操作多边形和圆形。对于 AABB,您应该能够简化此过程。

What the "real" physics engines do is find the minimum penetration vector. That is - the smallest vector that represents how far inside each other the two objects penetrate.

For an AABB (axis-aligned bounding box) this is really easy to calculate.

(Consider making your own 2D AABB structure, it will be smaller and therefore better for performance.)

Once you have your minimum penetration vector, you can perform collision response. And the easiest response is to simply separate the two objects by that vector (or separate the one object if the other is static).

Here is a good reference, by the makers of N on how to do this for convex polygons and circles. You should be able to simplify this down for AABBs.

清泪尽 2024-09-12 22:42:07

很大程度上取决于细节。完整的解决方案可以考虑速度或其他问题。但简单地对待这个......

你不想总是首先垂直解决。想象一下一个演员水平地推入一个方块。如果您首先垂直解析,那么演员将弹出该块上方,此时小的水平移动就足够了。

找出“避免碰撞”所需的垂直和水平移动,然后应用绝对值最小的移动。

然后重复几次,以防动作将演员带入另一个区块。但不要永远重复,因为演员可能会卡在某个永远无法解决的地方。

也许如果你的最后一个动作仍然让你发生碰撞,你可以平均最后两个动作并将其留在那里。

A lot depends on the details. A complete solution could take into account velocities or other issues. But treating this simply...

You don't want to always resolve vertically first. Imagine an actor nudges horizontally into a block. If you resolve vertically first then the actor will pop up above that block, when a small horizontal movement would have sufficed.

Find what the vertical and horizontal movements would need to be to "get out of collision", and then apply the one with the smallest absolute value.

Then repeat a few times, in case the movement puts the actor into another block. But don't repeat forever, because the actor could be wedged somewhere that never resolves.

Maybe if your last movement still leaves you in collision, you could just average the last two movements and leave it there.

巷子口的你 2024-09-12 22:42:07

另一种可能的方法是,不要等待碰撞然后移出,而是在移动对象之前检查可能的碰撞,如果将要发生碰撞,则仅将玩家移动到块的边缘。

即,对比 Adventure 如何与墙壁发生碰撞:http://www.youtube.com/观看?v=I6-zN_eaRd8
大多数 NES 游戏如何与墙壁发生碰撞。

Another possible approach is, rather than waiting for a collision and then shifting out, check for possible collisions before you move objects and, if there is going to be a collision, move the player only up to the edge of the block.

I.e., contrast how Adventure does collisions with walls: http://www.youtube.com/watch?v=I6-zN_eaRd8
to how most NES games do collisions with walls.

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