Page 1 of 1

Linking two bodies.

Posted: Sun Apr 01, 2012 10:26 pm
by zhaoling_0309
Hi, everyone.

I am going to make two bodies linked to each other,so moving together, but rotating independently.
Here are some codes that I have tried to implement these bodies.

mainSprite = [CCSprite spriteWithSpriteFrame:[frameCache spriteFrameByName:@"character.png"]];
mainSprite.scale = 0.6f;
[parentLayer addChild:mainSprite z:Z_CHARACTER];

basketSprite = [CCSprite spriteWithSpriteFrame:[frameCache spriteFrameByName:@"basket.png"]];
[parentLayer addChild:basketSprite z:Z_CHARACTER];

// Character Body
body = cpBodyNew(10000.0f, INFINITY);
bodyShape = cpCircleShapeNew(body, 45.0f, cpvzero);
cpSpaceAddBody(space, body);
cpSpaceAddShape(space, bodyShape);
cpShapeSetCollisionType(bodyShape, CHARACTER_COLLISION_GROUP);
cpShapeSetFriction(bodyShape, 0.2f);
cpBodySetPos(body, _initPos);
cpShapeSetGroup(bodyShape, CHARACTER_GROUP);
cpBodySetUserData(body, mainSprite);

// Basket
basketBody = cpBodyNew(10.0f, cpMomentForBox(100, 150, 15));
cpSpaceAddBody(space, basketBody);
cpVect verts1[] = { ccp(0, 30), ccp(10, 30), ccp(20, 0), ccp(10, 0) };
cpVect verts2[] = { ccp(0, 10), ccp(100, 10), ccp(100, 0), ccp(0, 0) };
cpVect verts3[] = { ccp(90, 30), ccp(100, 30), ccp(90, 0), ccp(80, 0) };
cpShape *testShape1 = cpPolyShapeNew(body, 4, verts1, ccp(-25, 50));
cpShape *testShape2 = cpPolyShapeNew(body, 4, verts2, ccp(-25, 50));
cpShape *testShape3 = cpPolyShapeNew(body, 4, verts3, ccp(-25, 50));
cpSpaceAddShape(space, testShape1);
cpSpaceAddShape(space, testShape2);
cpSpaceAddShape(space, testShape3);
cpShapeSetFriction(testShape1, 100000.0f);
cpShapeSetFriction(testShape2, 100000.0f);
cpShapeSetFriction(testShape3, 100000.0f);
cpShapeSetGroup(testShape1, CHARACTER_GROUP);
cpShapeSetGroup(testShape2, CHARACTER_GROUP);
cpShapeSetGroup(testShape3, CHARACTER_GROUP);
cpShapeSetCollisionType(testShape1, CHARACTER_BASKET_COLLISION_TYPE);
cpShapeSetCollisionType(testShape2, CHARACTER_BASKET_COLLISION_TYPE);
cpShapeSetCollisionType(testShape3, CHARACTER_BASKET_COLLISION_TYPE);
cpBodySetUserData(basketBody, basketSprite);

But i have couple of troubles.
1st, Basket sprite and basket body don't link up with each other.
2nd, Controlling angle velocity don't work on basket body.

Have you any idea about this?

Thanks.

Re: Linking two bodies.

Posted: Mon Apr 02, 2012 9:54 am
by slembcke
Did you add a joint anywhere? You'll need a pivot joint if you want them to move together.

What do you mean you can't control the angular velocity of the basket?

Re: Linking two bodies.

Posted: Mon Apr 02, 2012 10:02 am
by zhaoling_0309
I mean I can't control the rotation of basket body with cpBodySetAngVel.

And thanks for your reply.
I will try pivot joint.

Regards.