In our first physics recipe, we will explore the basics of creating a Box2D project and setting up a Box2D world. The example creates a scene that allows the user to create realistic 2D blocks.
The first thing we need to do is create a Box2D project using the built-in Box2D project template:
Go to File | New Project.
Under User Templates click on Cocos2d.
Now, right click on Cocos2d Box2d Application.
Click Choose, name your project, and hit Save.
Now, execute the following code:
#import "Box2D.h" #import "GLES-Render.h" //32 pixels = 1 meter #define PTM_RATIO 32 @implementation Ch4_BasicSetup -(CCLayer*) runRecipe { [super runRecipe]; /* Box2D Initialization */ //Set gravity b2Vec2 gravity; gravity.Set(0.0f, -10.0f); //Initialize world bool doSleep = YES; world = new b2World(gravity, doSleep); world->SetContinuousPhysics(YES); ...