# Box2d prismatic + revolute joints

Box2d prismatic + revolute joints
0

#1

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.

#2

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.

#3

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)
{
mWagonList[i-1]->getBody()->SetFixedRotation(true);
}

_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);
bodyStart->CreateFixture(&fixture);

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

b2Vec2 axis(-1.5f, 0.0f);
axis.Normalize();

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 https://www.raywenderlich.com/2722-how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-2

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));