However, for this tutorial we’re going to stay as-is. Add this after your init method: The first thing we do here is to call the “Step” function on the world so it can perform the physics simulation.
The two parameters here are velocity iterations and position iterations – you should usually set these somewhere in the range of 8-10.
It also has some elements of the sample project that is included in the cocos2d Box2d Application template, but explains how things work step-by-step.
So we don’t want to pass pixels in, because even small objects would be 60×60 pixels, way bigger than the values Box2D has been optimized for.
So we need to have a way to convert pixels to “meters”, hence we can just define a ratio like the above.
If you compile and run this template, you’ll see a pretty cool example demonstrating many aspects of Box2D.
However, for the purposes of this tutorial, we’re going to create everything from scratch so we can gain a good understanding of how things work.
There are a bunch of things you need to do to create a body – create a body definition, a body object, a shape, a fixture definition, and a fixture object. Once you’ve added all of the bodies you like to your world, Box2D can take over and do the simulation – as long as you call its “Step” function periodically so it has processing time.
But note that Box2D updates its internal model of where objects are – if you’d like the Cocos2D sprites to update their position to be in the same location as the physics simulation, you’ll need to periodically update the position of the sprites as well.Ok great – now let’s start creating our Box2D scene.Before we go any further, let’s talk a bit about how things work in Box2D.This tutorial helps get you started with Box2D with Cocos2D by showing you how to create a simple app that shows a ball that you can bounce around the screen by rotating your i Phone with the accelerometer.This tutorial is based on an excellent example by Kyle from i Phone Dev.net, but updated to the latest version of Cocos2D and with some more detailed explanations of how things work.The next thing we do is make our sprites match the simulation.