Game Development Reference
In-Depth Information
walls[i]-> u = 1.0f;
cpSpaceAddStaticShape(space, walls[i]);
}
}
Unlike Box2D, with Chipmunk you don't have to take any pixel-to-meter ratio into ac-
count. You can use the screen size in pixels as it is to define the corner points and to
work with Chipmunk bodies in general.
Four wall segments are added to the walls array. Each is a segment shape, which in
essence is just a line from A to B that blocks physics objects. You create the walls as
static shapes with the cpSpaceAddStaticShape method, but not before setting
some parameters on the wall. This is where Chipmunk gets a little too lazy for my
taste. It takes some getting used to to remember that e stands for elasticity (bounciness)
and u of all things means friction. Both values are coefficients, meaning if you set
them to 1 they will not be elastic and will cause no friction.
Adding Ticky-Tacky Little Boxes
To add boxes to the world, you use the same code in the init method of Hel-
loWorldLayer as in the Box2D example. Refer to Listing 12-4 to refresh your
memory.
I'll go straight to creating the dynamic body for new boxes, which is what the ad-
dNewSpriteAtPosition method does ( Listing 12-10 ) .
Listing 12-10. Adding a Body with a Sprite, Chipmunk Style
-(void) addNewSpriteAtPosition:(CGPoint)pos
{
int idx = CCRANDOM_0_1() * TILESET_COLUMNS;
int idy = CCRANDOM_0_1() * TILESET_ROWS;
CGRect tileRect = CGRectMake(TILESIZE * idx, TILESIZE * idy, TILESIZE, TILESIZE);
PhysicsSprite* sprite = [PhysicsSprite spriteWithTexture:spriteTexture
rect:tileRect];
sprite.position = pos;
 
 
Search Nedrilad ::




Custom Search