Hi there!
I'm exploring Cocos2D (with Chipmunk integrated) and it looks really interesting! However it's hard to understand how it works because I'm not able to find any kind of method's documentation and the methods itself aren't very explicit with their parameters (of course I know there are lots of tutorial that begin from the very beginning, but I just want documentation for some specific methods). I have a game scene with a square affected by gravity and another surface that is not affected by gravity (so it's a solid platform). The problem is that the dynamic square doesn't "touch" the static one and it goes through it as it doesn't even exists... How can you make a shape, let them static in the screen, but make it affect other shapes?
Here is how I modified the method to difference between a static and a dynamic shape:
Code:
-(void) addNewSpriteX: (float)x y:(float)y tag:(int)t isDynamic:(BOOL)dynamic
{
int posx, posy;
int width, height;
posx = 0;
posy = 0;
width = 0;
height = 0;
CGPoint verts[4];
if (t == kTagAtlasSpriteSheetBox) {
width = 32;
height = 32;
verts[0] = ccp(-16,-16);
verts[1] = ccp(-16, 16);
verts[2] = ccp( 16, 16);
verts[3] = ccp( 16,-16);
}else if (t == kTagAtlasSpriteSheetBlock) {
width = 32;
height = 32;
verts[0] = ccp(-16,-16);
verts[1] = ccp(-16, 16);
verts[2] = ccp( 16, 16);
verts[3] = ccp( 16,-16);
}
CCSpriteSheet *sheet = (CCSpriteSheet*) [self getChildByTag:t];
CCSprite *sprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(posx, posy, width, height)];
[sheet addChild: sprite];
sprite.position = ccp(x,y);
int num = 4;
if (dynamic) {
// New DYNAMIC object
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
// TIP:
// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
// cpVect == CGPoint
body->p = ccp(x, y);
cpSpaceAddBody(space, body);
cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
shape->e = 0.5f; shape->u = 0.5f;
shape->data = sprite;
cpSpaceAddShape(space, shape);
}else {
// New STATIC object
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
// TIP:
// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
// cpVect == CGPoint
body->p = ccp(x, y);
cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
shape->e = 1.0f; shape->u = 1.0f;
shape->data = sprite;
cpSpaceAddStaticShape(space, shape);
}
}
A shape declared as static is not affected by gravity, but it doesn't interact with dynamic shapes... Anyone knows how this works?
Thanks in advance!