Game Development Reference
In-Depth Information
height, so the coordinates need to be divided by 2—or, as in this case, set to 0.5f to cre-
ate a box shape whose sides are 1 meter wide and high.
The dynamic body also needs a fixture that contains the body's essential paramet-
ers—first and foremost, the shape, but also the density, friction, and restitution—which
influence how the body moves and bounces around in the world. Consider the fixture
to be a set of physics parameters used by bodies.
Updating the Box2D World
The physics bodies won't do anything unless you regularly call the Step method of
the Box2D world. You do this in the update method, shown in Listing 12-5 .
Listing 12-5. Updating Each Body's Sprite Position and Rotation
-(void) update:(ccTime)delta
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
world-> Step(delta, velocityIterations, positionIterations);
}
You animate the Box2D world by regularly calling the Step method. It takes three
parameters. The first is timeStep , which tells Box2D how much time has passed
since the last step. It directly affects the distance that objects will move in this step. Al-
though you're using the time delta here, passing the delta time as timeStep isn't
recommended, because the delta time fluctuates, and so the speed of the physics bodies
won't be constant. This effect rears its ugly head when the device may be taking a tenth
of a second to do background processing, such as sending or receiving an e-mail in the
background. This can make all physics objects move large distances in the next frame.
In a game, you'd rather have the game slow down or even stop for a tenth of a second
and then carry on where you left off. Without a fixed time step, the physics engine
would try to cope with a short interruption by moving all objects based on the time dif-
ference. If the time difference is large, the objects will move a lot more in a single
frame, and that can lead to them suddenly moving a large distance.

Custom Search