Fixing two box2D bodies together securely

2019-02-27 21:55发布

问题:

I am trying to join two box2d bodies together that are separated over a fixed distance. Both bodies cannot rotate themselves, and the join should have to rotation either. The gap between the bodies needs to allow other bodies to pass through.

I currently have a b2revoluteJoint setup like so:

b2RevoluteJointDef rjd;
rjd.lowerAngle = 0.0f;
rjd.upperAngle = 0.0f;
rjd.Initialize(body2, body1, body2->GetPosition());
rjd.collideConnected = false;
world->CreateJoint(&rjd);

However the joint is not completely rigid and the bodies tend to move around a fair bit relative to each other. Is there a better way to do this?

I have also tried the b2WeldJoint which did not work as I assume both bodies have to be overlapping...

EDIT:

The b2WeldJoint I have tried is:

b2WeldJointDef wj;
wj.Initialize(body1, body2, body1->GetWorldCenter());
world->CreateJoint(&wj);

However when I move one body, the other body stays in its position.

回答1:

I have also tried the b2WeldJoint which did not work as I assume both bodies have to be overlapping...

As far as I know, the shapes of the bodies do not necessarily have to overlap. As far as I have understood your setup, the weld joint would be appropriate, since it forbids all relative translation and rotation.