Game Development Reference
In-Depth Information
pos.x + = TILESIZE * posOffset;
cpBody* bodyC = cpBodyNew(mass, moment);
bodyC-> p = pos;
cpSpaceAddBody(space, bodyC);
shape = cpPolyShapeNew(bodyC, numVertices, vertices, offset);
cpSpaceAddShape(space, shape);
PhysicsSprite* spriteC = [self createPhysicsSpriteAt:pos];
[spriteC setPhysicsBody:bodyC];
bodyC-> data = (__bridge void*)spriteC;
// Create the joints and add the constraints to the space
cpConstraint* constraint1 = cpPivotJointNew(staticBody, bodyA, staticBody-> p);
cpConstraint* constraint2 = cpPivotJointNew(bodyA, bodyB, bodyA-> p);
cpConstraint* constraint3 = cpPivotJointNew(bodyB, bodyC, bodyB-> p);
cpSpaceAddConstraint(space, constraint1);
cpSpaceAddConstraint(space, constraint2);
cpSpaceAddConstraint(space, constraint3);
In this example, you're creating a pivot joint with cpPivotJointNew , which is the
same as the b2RevoluteJoint used in the Box2D example. Each joint is created
with the two bodies that should be connected to each other and one of the bodies' cen-
ter position as the anchor point. The cpPivotJointNew method returns a cpCon-
straint pointer, which you'll have to add to the space using the cpSpaceAd-
dConstraint method.
In this chapter, you learned the basics of the two physics engines distributed with
cocos2d: Box2D and Chipmunk. You now have two working examples of these phys-
ics engines at your disposal, which should help you decide which one you'd like to use.
Search Nedrilad ::

Custom Search