像素碰撞追踪

发布于 2024-12-03 22:28:40 字数 156 浏览 1 评论 0原文

我有一个 20 x 10 像素大的角色,并且有一个基于像素的碰撞图(想想蠕虫)。

给定每帧大于 1 像素的速度,跟踪角色碰撞的最佳方法是什么。有没有比沿着速度矢量迭代每个像素更好的解决方案?

我正在 Lua 中执行此操作(Love 2D),但通用解决方案将是理想的。

I have a character that's say, 20 by 10 pixels large and I have a collision map based on pixels (think worms).

What's the best way to trace collision for the character given a velocity greater than 1 pixel per frame. Is there a solution better than iterating through each pixel along the velocity vector?

I'm doing this in Lua (Love 2D) but a generic solution would be ideal.

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

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

发布评论

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

评论(2

谎言 2024-12-10 22:28:40

我会将边界框碰撞和像素完美碰撞结合起来。

因此,游戏中的所有实体都将具有边界框,即等于精灵的宽度和高度的框架。将此作为您的第一级碰撞测试。完成此操作后,您将发生碰撞,然后使用碰撞贴图获得更精细的细节。

这种优化将有助于提高速度,并增加引擎的灵活性,即并非所有碰撞都必须是像素完美的。

至于实际的像素完美碰撞算法,您所描述的将起作用。但是,如果您追求速度,您可能想尝试以下操作:

为每个精灵提供一个位掩码(类似于像素图,但每个像素只有一位)
例如:

00000000
00100000
01100000
01110000

当一个精灵与另一个精灵碰撞时,使用与较大精灵大小相同的较小位掩码创建一个新的位掩码,并根据精灵之间的位置差异对其进行“偏移”。

完成此操作后,按位“与”这两个掩码中的所有字节。如果任何字节结果> 0,发生碰撞。

I would combine bounding box collision and pixel perfection collision.

So all entities in your game would have bounding boxes, just frames equal to the width and height of your sprite. Use this as your first level collision test. After this is done, and you have a collision, then use the collision maps to get a finer level of detail.

This optimization is going to help with speed, and adds the flexibility to the engine that not all collisions have to be pixel perfect.

As for the actual pixel perfect collision algorithm, what you describe would work. However, if you are going for speed, you might want to try this:

come up with a bitmask for each sprite (like a pixel map, but only one bit per pixel)
for example:

00000000
00100000
01100000
01110000

when one sprite collides with another, create a new bitmask out of the smaller bitmask of the same size of the larger one, and 'offset' it by the difference in position between sprites.

Once this is done, bit-wise 'and' all the bytes in these two masks. If any byte result > 0, you have a collision.

枕梦 2024-12-10 22:28:40

您的解决方案是最简单的 - 迭代每个像素。

只需确保每次迭代时仅检查“新”像素。

假设角色同时向右和向下移动:

*****   .....       .....        * = "Present"
*****   .*****      .****#       . = "Old and now empty"
*****   .*****  =>  .****#        # = "New"; check these on iteration 2
*****   .*****      .****#
         *****       #####

It. 1   It. 2      "New" pixels

在沿着移动的每次迭代中,要检查的像素几乎没有差异;只有标记为“新”的才需要检查影响。检查这些,如果没有碰撞,则继续移动。您可以使用它来优化大量计算。

Your solution is the simplest one - iterate over each pixel.

Just make sure that you only check the "new" pixels on each iteration.

Say the character is moving to the right and down at the same time:

*****   .....       .....        * = "Present"
*****   .*****      .****#       . = "Old and now empty"
*****   .*****  =>  .****#        # = "New"; check these on iteration 2
*****   .*****      .****#
         *****       #####

It. 1   It. 2      "New" pixels

On each iteration along the movement there's little difference on the pixels to check; only the ones marked as "new" need checking for impact. Check on those, and if there's no collision, continue moving. You can use that to optimize away lots of calculations.

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