Game Development Reference
In-Depth Information
The array of vertices are the corners of the box shape. Because the corner positions are
positioned relative to the center of the box you're creating, they're all half a tile's size
away from the center. Otherwise, the box shape would be twice as big as the tile.
The cpPolyShapeNew method then takes the body as input, the verts array, and
the number of vertices in the array, as well as an optional offset, which is set to
CGPointZero in this case. Out comes a new cpShape pointer for the box shape.
The shape's elasticity and friction are set to values that give a similar behavior to the
Box2D boxes, and after the sprite is set as user data to the data field, the shape is added
to the space via cpSpaceAddShape . And finally, there's also a Chipmunk version of
the PhysicsSprite class which gets the body assigned so it can update its position
and rotation based on the Chipmunk body.
The Chipmunk body also gets the sprite assigned to its data field. Again this requires
a bridge cast so ARC won't complain.
Updating the Chipmunk Space
You have to update the Chipmunk space every frame to advance the physics simula-
tion. You do this in the update method in Listing 12-11 .
Listing 12-11. Updating the Chipmunk Space
-(void) update:(ccTime)delta
const int iterations = 10;
for (int i = 0; i < iterations; i++)
cpSpaceStep(space, 0.005f);
Just as with Box2D, you have to advance the physics simulation using a step method.
In this case, it's cpSpaceStep , which takes the space and a time step as input. A
fixed time step works best, and just like in Box2D, using a fixed time step as opposed
to passing the delta time is highly recommended. This is probably even truer for Chip-
munk, as I noticed very odd effects when using the delta time. As long as the framerate
Search Nedrilad ::

Custom Search