iPhone 上 2D 碰撞检测的最佳方法是什么?
一直致力于解决碰撞检测问题,我可以采取 3 种主要方法:
Sprite 和 mask 方法。 (以及精灵的重叠,并检查生成的精灵像素数据中的非零数字)。
边界圆、矩形或多边形。 (创建一个或多个包围精灵的形状,并进行基本数学计算以检查是否重叠)。
使用现有的精灵库。
第一种方法,尽管这就是我在过去 16x16 精灵块的做法,但似乎没有一种简单的方法来获取单个图像像素数据和/或 Alpha 通道在 Quartz(或 OPENGL)内。检测边界框的重叠很容易,但是从重叠创建第三个图像,然后测试它的像素就很复杂,而且我的直觉是,即使我们能让它工作也会很慢。我在这里缺少一些整洁的东西吗?
第二种方法是将我们的精灵分成几个多边形并测试它们的重叠。多边形越多,碰撞检测就越准确。好处是速度快,而且准确。缺点是它使精灵创建变得更加复杂。即,我们必须为每个精灵创建多边形。为了提高速度,最好的方法是创建多边形树。
我不确定第三种方法,因为它涉及购买代码(或使用开源许可证)。我不确定最好使用的库是什么,或者这是否会让生活更轻松,或者给我们带来将其集成到我们的应用程序中的问题。
简而言之,我赞成多边形和树方法,并且在我编写大量代码之前,希望您对此发表看法。
最好的问候
戴夫
Been working on this problem of collision detection and there appears to be 3 main approaches I could take:
Sprite and mask approach. (AND the overlap of the sprites and check for a non-zero number in the resulting sprite pixel data).
Bounding circles, rectangles or polygons. (Create one or more shapes that enclose the sprites and do the basic maths to check for overlaps).
Use an existing sprite library.
The first approach, even though it would have been the way I would have done it in the old days of 16x16 sprite blocks, it appears that there just isn’t an easy way of getting at the individual image pixel data and/or alpha channel within Quartz (or OPENGL for that matter). Detecting the overlap of the bounding box is easy, but then creating a 3rd image from the overlap and then testing it for pixels is complicated and my gut feel is that even if we could get it to work would be slow. Am I missing something neat here?
The second approach involves dividing up our sprites into several polygons and testing them for overlaps. The more polygons the more accurate the collision detection. The benefit is that it is fast, and can be accurate. The downside is it makes the sprite creation more complicated. i.e., we have to create the polygons for each sprite. For speed the best approach is to create a tree of polygons.
The 3rd approach I’m not sure about as it involves buying code (or using an open source licence). I am not sure what the best library to use is or whether this would make life easier or give us a problem integrating this into our app.
So in short I am favouring the polygon and tree approach and would appreciate you views on this before I go and write lots of code.
Best regards
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑将你的精灵多边形化。在“后台”保留精灵的多边形表示,并使用游戏世界的多边形版本进行碰撞测试。这会产生一些开销和额外的复杂性,但就额外的可能性而言通常是非常有益的。 (动态阴影、光照效果、独立于精灵图形的碰撞检测、更多碰撞检测方法、多边形相交等)
否则您将陷入您所描述的基于像素的方法,这也不错。
(对重叠进行“或”运算)您可以自己执行此操作,也可以让 OpenGL 在帧缓冲区中执行此操作。 (我不记得这些电话,认为它被称为 z 掩蔽(?),......抱歉。
对于小精灵,这甚至可能会更慢......)考虑使用二进制空间分区技术来优化其中的一些:
例如,四叉树,允许您快速找到用于碰撞测试的候选精灵。它们适用于基于像素和顶点的方法。
或者只使用 BSPTree。
顺便说一句:我认为碰撞检测的像素精度实际上是一个很好的功能,我在 Jump'n Runs 中经常使用它。不过,可能不适用于您的游戏。
Consider polygonizing your sprites. Keep a polygonal representation for your sprites "in the background" and do collision testing with the polygonized version of your game world. This produces some overhead and additional complexity, but is often very rewarding in terms of additional possibilities. (dynamic shadows, lighting effects, collision detection independent of sprite graphics, many more collision detection methods, polygonal intersection etc.)
Otherwise you are stuck with the pixel based approach you described, which is also not bad.
(ORing the overlap) You can just do it on your own or let OpenGL do it in the frame buffer. (I can't remember the calls, think it was called z-masking (?), ... sorry.
For small sprites this might even be slower...) Consider using binary space partitioning techniques in order to optimize some of it:
Quad trees for example, allow you to quickly find the candidate sprites for collision testing. They are applicable on both the pixel and vertex based approach.
Or just use BSPTrees.
Btw: I think pixel precision on collision detection is actually a nice feature, I used it a lot in Jump'n Runs. Maybe not applicable to your game, though.