开发者

What is the best way to handle collision detection between bodies efficiently?

I am new to Cocos2d, Box2开发者_开发技巧d and game development all together, but I have read a good bit of tutorials to at least have a good start on a game set up and working...

Im now at the point where I need to start adding more bodies to a layer and need to check and see if and when my main avatar will collide with any of them..

Common sense seems to tell me that the more bodies I add and the more cases I add checking to see if fixture1 is colliding with fixture2 for instance will bog down the processor at some point..

Are there any best practices and/or efficient algorithms to make these checks more efficient over time as the number of bodies grow?

Any links or direction would be appreciated! thank you!


You can use QuadTree to divide the scene and get a list of bodies that needs to be checked. (There are a lot of articles shows how QuadTree works, just google it:D )

And if that's a little complex for you. Then you can try to divide your scene into many grid, and make a loop to put bodies in their grid based on their 2d position. Then just check bodies in each grid. It's a lot faster than normal loop.

http://i.stack.imgur.com/W5cBT.png


As of iOS 7 you can use Sprite Kit to handle collisions:

https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html#//apple_ref/doc/uid/TP40013043-CH6-SW14

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜