Linking two bodies.

Official forum for the Chipmunk2D Physics Library.
Post Reply
zhaoling_0309
Posts: 2
Joined: Sun Apr 01, 2012 8:00 pm
Contact:

Linking two bodies.

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Linking two bodies.

Post 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?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
zhaoling_0309
Posts: 2
Joined: Sun Apr 01, 2012 8:00 pm
Contact:

Re: Linking two bodies.

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests