1 - (void)addCrateWithFrame:(CGRect)frame
2 {
3 Crate *crate = [[Crate alloc] initWithFrame:frame];
4 [self.containerView addSubview:crate];
5 cpSpaceAddBody(self.space, crate.body);
6 cpSpaceAddShape(self.space, crate.shape);
7 }
8
9 - (void)addWallShapeWithStart:(cpVect)start end:(cpVect)end
10 {
11 cpShape *wall = cpSegmentShapeNew(self.space->staticBody, start, end, 1);
12 cpShapeSetCollisionType(wall, 2);
13 cpShapeSetFriction(wall, 0.5);
14 cpShapeSetElasticity(wall, 0.8);
15 cpSpaceAddStaticShape(self.space, wall);
16 }
17
18 - (void)viewDidLoad
19 {
20 //invert view coordinate system to match physics
21 self.containerView.layer.geometryFlipped = YES;
22 //set up physics space
23 self.space = cpSpaceNew();
24 cpSpaceSetGravity(self.space, cpv(0, -GRAVITY));
25 //add wall around edge of view
26 [self addWallShapeWithStart:cpv(0, 0) end:cpv(300, 0)];
27 [self addWallShapeWithStart:cpv(300, 0) end:cpv(300, 300)];
28 [self addWallShapeWithStart:cpv(300, 300) end:cpv(0, 300)];
29 [self addWallShapeWithStart:cpv(0, 300) end:cpv(0, 0)];
30 //add a crates
31 [self addCrateWithFrame:CGRectMake(0, 0, 32, 32)];
32 [self addCrateWithFrame:CGRectMake(32, 0, 32, 32)];
33 [self addCrateWithFrame:CG