0
votes

Hello everybody i'm developing a game with cocos2d v1 and i'm using box2d. i'm enabling the retina display. and i'm creating a bucket. i want to move my bucket only in the x axis so i did a b2PrismaticJoint but the problem is that the bucket doesn't fit with the ground. here is my code : `CCSprite *tube = [CCSprite spriteWithFile:@"tube.png"]; tube.position = ccp(SCREEN_WIDTH/2, SCREEN_HEIGHT/2); [self addChild:tube];

b2BodyDef bucketBodyDef;
bucketBodyDef.type = b2_dynamicBody;
bucketBodyDef.position.Set(tube.contentSize.width/PTM_RATIO, tube.contentSize.height/PTM_RATIO);
bucketBodyDef.userData = tube;
tubeBody = world->CreateBody(&bucketBodyDef);

b2PolygonShape bucketShape;
bucketShape.SetAsBox(tube.contentSize.width/2/PTM_RATIO, tube.contentSize.height/2/PTM_RATIO);


// Create shape definition and add to body
b2FixtureDef bucketShapeDef;
bucketShapeDef.shape = &bucketShape;
bucketShapeDef.density = 10.0f;
bucketShapeDef.friction = 0.4f;
tubeFixture = tubeBody->CreateFixture(&bucketShapeDef);


[self createTubeFixtures];


// Restrict bucket along the x axis
b2PrismaticJointDef jointDef;
b2Vec2 worldAxis(1.0f, 0.0f);
jointDef.collideConnected = true;
jointDef.Initialize(tubeBody, groundBody,
                    tubeBody->GetWorldCenter(), worldAxis); 
world->CreateJoint(&jointDef);`

When i remove the joint all is good..please see the picture attached. thanks!

With joint

Without joint

1

1 Answers

0
votes

I did a little mistake that's cost me two days!! and that was on setting the coordinates of the body : b2BodyDef bucketBodyDef; bucketBodyDef.type = b2_dynamicBody; **bucketBodyDef.position.Set(tube.contentSize.width/2/PTM_RATIO, tube.contentSize.height/2/PTM_RATIO);** bucketBodyDef.userData = tube; tubeBody = world->CreateBody(&bucketBodyDef);