使用 OpenGL 对 iPhone 上的图像进行碰撞检测
我正在为 iPhone 开发一款 2D 游戏(有点像自上而下的太空射击游戏),使用的引擎与 OpenGL ES 上的 cocos2d 非常相似(但不完全一样)。我正在尝试弄清楚如何进行碰撞检测。
我的游戏中的所有船只都是图像,游戏会将图像作为纹理加载到屏幕上。我已经进行了非常非常简单的检测,基本上只是获取图像的矩形并检查它们是否发生碰撞并且可以很好地做到这一点。
但是,当然,这艘船并没有完全占据整个矩形,因此其中存在空白。所以我的问题是我应该如何解释该空白?我必须存储船只的矩阵吗?或者还有别的办法吗?我还听说可能使用花栗鼠物理引擎进行碰撞检测?那会如何运作呢?
I'm working on a 2D game (kind of like a top down space shooter) for the iPhone using an engine very similar to cocos2d (not exactly though) on OpenGL ES. I'm trying to figure out how I'm going to do collision detection.
All the ships for my game are images, and the game will load the image as a texture onto the screen. I've got very very simple detection going already that basically just takes the rectangles of the images and checks to see if those collide and can do that just fine.
But, of course the ship isn't perfectly taking up the entire rectangle so there is whitespace in there. So my question is how am I supposed to account for that whitespace? Do I have to have the matrices of the ships stored? Or is there another way? I've also heard of possibly using the Chipmunk physics engine for collision detection? How would that work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(1) 关于 Chipmunk,简短的回答是是的,您应该立即下载 Chipmunk,向他捐赠一些东西,并开始了解它。
花一天左右的时间基本上就能回答你所有的问题。如果你想玩物理游戏,你就需要参与其中。
(2)你询问使用近似值(“只是”一个矩形)而不是像你的宇宙飞船那样更精确形状的东西。 事实上,您可能会惊讶地发现,这正是您从我们小时候玩过的所有著名大牌游戏中通常所做的事情孩子们!事实上,有时您可能只使用一个点(!)来检测碰撞。
在生产中您可能会做的是尝试一个更复杂的模型,然后玩几个小时,看看它是否真的比简单的点或矩形模型更好玩。
如果您确实想制作一个更复杂的模型 - 只需制作一个!使用您当前的系统从三个或四个矩形构建它。尝试“全部相互对抗”,并“首先检查一个大的”,看看它是否彼此接近(有点简单的空间散列)。
您会发现,当您使用 Chipmunk 执行此操作时(您现在知道您必须在阅读此消息后立即开始),您只是以同样乏味的方式构建它。这不是灵丹妙药。但是,如果您要使用“更复杂的模型”,是的,最好使用标准的花栗鼠来完成工作 - 它会完成得更快更好。有很多东西需要学习,你应该跳起来!
(3) Unity 不仅仅适用于 3D 最后,如果您想以聪明的方式做这件事,您必须使用 Unity3D,它可以让您访问芯片组上的金属、Nvidia 物理特性。请注意,Unity 也适用于 2D 游戏 - 您只需单击 Unity 中的一个按钮即可使用 2D 投影(许多品牌 ifone 2D 游戏都是这样做的)。
如果您使用这种方法,您可以(如果您愿意)拥有“绝对精确”的物理学,对模型的每个角落和缝隙进行建模。
这样做有什么缺点?啊哈...好吧,问题是,您需要游戏中所有内容的精美实际 3D 模型! (就像您在您最喜欢的皮克斯蓝光光盘附带的“我们如何制作电影”特殊功能中看到的那样。)为此,您需要像 autodesk、maya 之类的东西。你很可能会从数字道具商店购买一些现成的模型(不需要建造“一把椅子”,因为它已经做了 1000 次,你可以花 10 美元买一把)。
(Unity3D 完全免费使用几个月,您可以看看它是否可以赚钱。)
顺便说一句,在 Chipmunk 方面 --- 您可以使用 Corona,它非常容易使用,并且具有类似花栗鼠的物理特性完全内置,您无需付出任何努力!您可以在比写这封电子邮件更短的时间内完成整个游戏。您可能已经在销售您的游戏并正在考虑下一款游戏。或者,你可以使用“Cocos”,它确实内置了一个类似花栗鼠的物理库..就我个人而言(只是我)我不喜欢也不会碰cocos - 但当然很多游戏都使用它。
(对我来说,使用 cocos 这种“适合白痴”的产品似乎毫无意义,而你可以直接使用 Corona,它是一种“适合白痴”的产品,但使用起来非常简单,坚固 1000 倍,完成产品并开始赚钱的速度可能会快 10 倍。)
Noel 摘要:
所以在某种意义上使用 Unity3D (因此,如果您想要详细的角落和缝隙碰撞,则计算机芯片上的实际 nvidia 物理特性是最终的解决方案。 再往下走一步,花栗鼠正是您应该在 ifone/ipad 上使用的 2D 物理引擎 - 它正是所有著名游戏中所使用的东西我们非常了解。你需要学习一些知识,所以跳起来吧——这非常有趣。 最后,如果您愿意,可以让您当前的模型变得更加复杂 - 通过添加更多矩形来推出您自己的模型!
第四点是,请务必记住,令人惊讶的是,在游戏中,您通常可以使用非常简单的物理(通常比一个矩形更简单!!该死的一点 - 即,简单地测量中心之间的距离!)第五,在竭尽全力测试更详细的物理之后,您将相互进行测试,并且找出你能逃脱的最简单的物理学是什么。
(1) regarding Chipmunk, the short answer is yes you should immediately download chipmunk, donate something to the bloke, and start learning about it.
Working with that for a day or so will basically answer all the questions you have. If you want to work with physics games you're going to need to get in to it.
(2) you ask about using an approximation ("just" a rectangle) instead of something more accurately shaped like your spaceships. In fact, you'll be perhaps amazed to learn, that is precisely how it is usually done in all your famous big-name games you've played since we were all kids! Indeed sometimes you might use little more than A DOT (!) to detect collisions.
What you'd probably do in production is try a more complicated model, and play with it for a few hours and see, is it actually any better to play with than your simple dot or rectangle model.
If you do want to make a more complicated model -- just make one! Build it up from three or four rectangles using your current system. Try them "all against each other", and have "one big one to check first" to see if it is even anywhere near each other (sort of a simple spatial hashing).
You will find that when you do it with Chipmunk, which as you now know you have to immediately begin after reading this message, you just build it up the same tedious way. It's not a magic bullet. But if you were going to use a "more complicated model" yes it is better to go with something standard, chipmunk, to do the work in - it will get done quicker and better. There is heaps to learn and you should hop to it!
(3) Unity is not just for 3D Finally if you want to do it the smart-ass grown up way, you'd have to use Unity3D which will let you access the very metal, the Nvidia physics on the chipset. Note that unity works perfectly for 2D games also - you just click one button in unity to use a 2D projection (many brand-name ifone 2D games are done exactly like that).
If you use that approach, you can (if you want) have "absolutely exact" physics, with every nook and cranny of your model modelled.
What is the downside to doing this? Ah hah ... well the thing is, you need superb actual 3D models of all the stuff in your game! (Like you see them building in the "how we made the movie" special features that come with your favourite Pixar blu-ray.) To do that you need things like autodesk, maya and the like. you would quite likely buy some models ready-made from a digital prop shop (no need to build "a chair" as it has been done 1000 times already and you can buy one for ten dollars).
(Unity3D is completely free to use for a few months while you see if it can make you money.)
Incidentally on the Chipmunk front --- you can just use Corona which is ridiculously easy to use and has chipmunk-like physics completely built in with zero effort on your part! You could have the whole game done in less time than it took to write this email. You could be selling your game already and thinking up the next one. Or, you could use "Cocos" which indeed has a chipmunk-like physics library built-in .. personally (just me) I do not like and won't touch cocos - but of course many games use it.
(It seems pointless, to me, using cocos which is a "for idiots" product, when you can just go ahead and use Corona, which is a "for idiots" product but stupendously easier to use, 1000x more solid, and probably literally 10x faster to finish your product and start making money.)
Noel Summary:
So in some sense using Unity3D (and hence, the actual nvidia physics on your computer's chips) is the ultimate solution if you want detailed nook-and-cranny collisions. Going down one step, Chipmunk is exactly, precisely what you should be using on the ifone/ipad for 2D physics -- it is precisely what is used in all the famous games we know so well. You have a bit of learning to do so hop to it - it's superfun. Finally go right ahead and just make your current model more complicated if you wish - roll your own by adding more rectangles!
And the fourth point is, be sure to remember that in games, astonishingly, you can often get away with remarkably simple physics (often SIMPLER!! than one rectangle - just a damn point - ie, simply measuring the distance between centers!) Fifthly after going to all the effort of testing more detailed physics, you would play test one against each other, and find out what is the simplest physics you can get away with.