When I first started integrating Box2D into my Cocos2D project, I found the whole process a little jarring. While I’d used C++ before, this was the first time I’d seen it mixed with Objective-C. This code snippet simply wraps the standard Box2D Debug calls from GLES-Render.h (included with Cocos2D) in it’s own CCLayer, so that you can implement it quickly and easily. It also allows you to turn the layer on an off easily as you would any other CCLayer.
Box2D is a great physics engine, but while there are some iOS code examples out there – there really aren’t too many tutorials around utilizing the iOS platform that also demonstrate the fundamental principles used by the engine.
I’ve found one tutorial that does a great job of explaining some of the more intermediate Box2D tutorial topics utilizing Cocos2D iPhone – specifically addressing Forces, Ray Casts and Sensors. I previously mentioned the first part of this tutorial which was created using LevelHelper and SpriteHelper (which are not required for this portion of the tutorial).
The tutorial demonstrates the creation of a Kid Icarus style platform game, and utilizes sensors and ray casts so the enemies can attack the game character with intelligence, and applies forces to keep the enemies floating in the air. The tutorial also demonstrates how to create a HUD overlay in Cocos2D for the game menu, and life tracker.
Here’s a video demonstrating the tutorial in action:
You can find the tutorial in 3 parts here – full source code is provided so you can jump right to the second part if desired:
Pt.1 How To Use SpriteHelper And LevelHelper
Pt.2 Intermediate Box2D Physics
Overall, I think it’s a great tutorial on building a platformer. Personally I really never thought of using Box2D for some of the functionality it’s used for in the tutorial, but it definitely makes things easier once you have a basic understanding.