0
votes

How can I connect theses two sprites? But not a joint where it moves but a solid connection where both of them stay together? Thank you :)

cart = [CCSprite spriteWithImageNamed:@"bottomCart.png"];
cart.position = ccp(self.contentSize.width/2, self.contentSize.height/3);
cart.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, cart.contentSize} cornerRadius:0];
cart.physicsBody.collisionGroup = @"cartGroup";
cart.physicsBody.collisionType = @"cartCollision";
cart.physicsBody.type = CCPhysicsBodyTypeDynamic;
[_physicsWorld addChild:cart];

wheel1 = [CCSprite spriteWithImageNamed:@"blackball.png"];
wheel1.position = ccp(self.contentSize.width/2, self.contentSize.height/3);
wheel1.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:wheel1.contentSize.width/2.0f andCenter:wheel1.anchorPointInPoints];
wheel1.physicsBody.collisionGroup = @"cartGroup";
wheel1.physicsBody.collisionType = @"cartCollision";
wheel1.physicsBody.type = CCPhysicsBodyTypeDynamic;
[_physicsWorld addChild:wheel1];
1

1 Answers

1
votes

Actually a joint is what you need.

You have to use something that is called a pin joint : CCPhysicsJoint.

Using a pin joint you connect two CCPhysicsBody objects in one single point.

In this use case though, unless you had a very good reason I would not use physics for the wheel at all. I would define the wheel to be a child of the cart and apply a CCActionRepeatForever + CCActionRotate combo on it to make it rotate.

For the physics to still hold up with the wheel I would define a larger polygon as a physics body on the parent cart with a visual tool like SpriteBuilder.