libgdxbox2drevolute-joints

libgdx+box2d joints not working properly under gravity


I am trying to create a pendulum using box2d/libgdx by connecting a static and a dynamic body. I have tried various types of joints like distance joint, rope joint and revolute joint but under gravity the dynamic body always ends up escaping down after initially being constrained by the joint. Only in zero gravity the joints work properly. I know I must be doing something really silly.

       world.getBodies(p);
       DistanceJointDef djd=new DistanceJointDef();
       djd.bodyA=p.get(0);
        djd.bodyB=p.get(1);
        djd.localAnchorA.set(0,0);
        djd.localAnchorB.set(0,0);
        djd.collideConnected=true;
               DistanceJoint pend=(DistanceJoint)world.createJoint(djd);   
        pend.setLength(50);

PS. I have to yet implemented a contact listener .I have not figured out how to do this. Will putting up a collision listener help?


Solution

  • If I get your question correctly, your moving body falls irrespective of the type of joint you applied. One reason why that could happen is because you may have defined both the anchor and pendulum as dynamic bodies, and both fall the same way due to gravity.

    I am assuming you are not yet rendering the anchor body, so you do not see it falling.