Game Development Reference
In-Depth Information
CCNode* batchNode = [self getChildByTag:kTagBatchNode];
[batchNode addChild:sprite];
const int numVertices = 4;
float halfTileSize = TILESIZE * 0.5f;
CGPoint verts[] =
ccp(−halfTileSize, -halfTileSize),
ccp(−halfTileSize, halfTileSize),
ccp(halfTileSize, halfTileSize),
ccp(halfTileSize, -halfTileSize),
float mass = 1.0f;
cpBody* body = cpBodyNew(mass, ←
cpMomentForPoly(mass, numVertices, verts, CGPointZero));
body-> p = pos;
cpSpaceAddBody(space, body);
cpShape* shape = cpPolyShapeNew(body, numVertices, verts, CGPointZero);
shape-> e = 0.4f;
shape-> u = 0.4f;
cpSpaceAddShape(space, shape);
[sprite setPhysicsBody:body];
body-> data = (__bridge void*)sprite;
You create the dynamic body for the box with the cpBodyNew method with the given
mass and a moment of inertia . The moment of inertia determines the resistance of a
body to move, and it's calculated by the helper method cpMomentForPoly , which
takes the body's mass and the vertices of its 32×32-pixel box shape.
The body's position, p , is then updated, and the body is added to the space via the
cpSpaceAddBody method. Note that contrary to Box2D, you don't have to convert
pixels to meters; you can work with pixel coordinates directly.
Search Nedrilad ::

Custom Search