Box2d prismatic + revolute joints

Box2d prismatic + revolute joints


I’m trying to unite train wagons, using revolute and prismatic joint with box2d

What I want to achieve is that each car is separated with a distance from the other
and that each joint has an angle of rotation limit.

I did not use the distance joint because I do not want the bodies to rotate when they are suspended.

Sorry for my bad english, I speak Spanish.
Thank you for your help.


Is there anything in ‘cpp-tests’ that demos what you need? There are a lot of Physics examples in there.

Side note: awesome image that you posted. I can totally see how your game will work. When you finish ping me so I can play it.


hey @slackmoehrle thanks for you reply!

As you mentioned earlier I was looking at the examples.

and I realized that by enabling the fixedRotation of the first wagon, I get what I want.

For a first instance I consider it acceptable, it would have to improve some values of the body def and the fixtures

Then I share a part of the code below

for (int i = 1; i <= nWagonCount - 1; i++)
		if (i == 1)

		_wagon =Wagon::create(mWorld);
		mWagonList[i]->getBody()->SetTransform(b2Vec2(mWagonList[i - 1]->getBody()->GetPosition().x - 0.85f, mWagonList[i - 1]->getBody()->GetPosition().y), mWagonList[i]->getBody()->GetAngle());

		b2BodyDef bdStart;
		bdStart.type = b2_dynamicBody;
		bdStart.position = mWagonList[i - 1]->getBody()->GetPosition();
		b2Body* bodyStart = mWorld->CreateBody(&bdStart);

		b2RevoluteJointDef rjd;
		rjd.bodyA = mWagonList[i - 1]->getBody();
		rjd.bodyB = bodyStart;
		b2Joint* r_joint = (b2RevoluteJoint*)mWorld->CreateJoint(&rjd);

		b2Vec2 axis(-1.5f, 0.0f);

		b2PrismaticJointDef pjd;
		pjd.bodyA = mWagonList[i]->getBody();
		pjd.bodyB = bodyStart;
		pjd.localAxisA = axis;
		pjd.localAnchorB = b2Vec2(-1.5f, 0.0f);
		pjd.lowerTranslation = -0.25f;
		pjd.upperTranslation =0.0f;
		pjd.enableLimit = true;
		b2Joint* p_joint = (b2PrismaticJoint*)mWorld->CreateJoint(&pjd);

although in the simulation it seems as if the car did not rotate.

I found a solution by watching the tutorial

Within the update of the vagon node that extends the Sprite class

With the following code, the sprite follows the orientation of the body

void Wagon::update(float delta)
	this->setPosition(ccp(mBody->GetPosition().x*PTM_RATIO, mBody->GetPosition().y*PTM_RATIO));
	b2Vec2 vel = mBody->GetLinearVelocity();
	float angle = ccpToAngle(ccp(vel.x, vel.y));
	this->setRotation(-1 * CC_RADIANS_TO_DEGREES(angle));

I hope it helps

When I made the game, I let you know