box2d-iphonerevolute-joints

Is it possible to join a body from two sides to different static bodies


    b2Body* foundation =[self createStaticBodyAtLocation:CGPointMake(15, 15) withSize:CGSizeMake(35, 35)];
    b2Body* beam=[self createDynamicBodyAtLocation:CGPointMake(105, 35) withSize:CGSizeMake(150, 10)];
    b2RevoluteJointDef revoluteJointDef;
    revoluteJointDef.Initialize(foundation, beam, b2Vec2(30.0/PTM_RATIO,30.0/PTM_RATIO));
    _world->CreateJoint(&revoluteJointDef);

The above code creates a static and a dynamic body and then joins them with a revolutejoint. Here's what I get:

Before adding the second static body

Then I add the following lines to add another body, which is a static circle and then join the circle and the previously added bar on the other end of the bar.

    b2Body *jointBall=[self createCircleAtLocation:CGPointMake(160, 135)];
    revoluteJointDef.Initialize(jointBall, beam, b2Vec2(100.0/PTM_RATIO,10.0/PTM_RATIO));
    _world->CreateJoint(&revoluteJointDef);

But here's what I get:

After adding the second static body

After adding the circle, I can't move the bar. It's fixed. But I'd expect it to join to the circle. I've tried to change the anchor point to different positions but it doesn't help. What am I missing? Maybe it is not possible to join a dynamic body to more than one static bodies.


Solution

  • From the box2d manual:

    All joints are connected between two different bodies. One body may be static.

    You may manually move static bodies, but you should be careful so that you don't squash dynamic bodies between two or more static bodies

    What you've effectively done here is squash the bar body between two static bodies with revolute joints. Static bodies cannot move at all, so the output you're seeing actually makes sense. The first revolute joint (connecting box and bar) allows the far end of the bar to move (rotationally). However, when this far end is connected to another static body by another joint that restricts all motion but rotation, it cannot move anymore. Thus, it makes sense that the rotation of your bar body will become fixed after joining it with two different static bodies.

    I searched through the box2d code and didn't find anything that directly sets the rotation of a body connected via a joint.

    What are you trying to do? It seems like you might be trying to have the ball move along with the end of the bar, in which case I would recommend making the ball a dynamic body (so it can move). Also, depending on what you're doing, a weld joint might be more appropriate.