Search code examples
androidlibgdxbox2d

Revolute joint in libgdx is not rotating back(not working) when limit reaches.How to control revolute joint based on user touch


I have been searching how to control revolute joint in libgdx with box2d based on user touch and revolute joint is being stopped after it reached upper angle.Is there any way to control revolute joint ?

   `
     jd = new RevoluteJointDef();
     jd.initialize(bodyPivot, boxBody, anchor);

    jd.lowerAngle = 0.75f * (float)3.14; // -90 degrees
    jd.upperAngle = 0.75f * (float)3.14; // 45 degrees
    jd.collideConnected=false;
    jd.enableLimit = true;
    jd.maxMotorTorque = 1000.0f;
    jd.enableMotor=false;
    jd.motorSpeed = 0f*(float)3.14;
    rj = (RevoluteJoint) world.createJoint(jd);`

I tried using rj.enableMotor(true) but It didn't work


Solution

  • When you create a joint the current relative angle between the bodies is taken to be zero when it comes to specifying the limits.

    If the joint keeps rotating in the same direction all the time then the limits don't actually change, because the new starting point is now zero as far as the limits are concerned.

    jointDef.upperAngle = MathUtils.PI;
    jointDef.lowerAngle = 0;//the position when joint was created
    

    But if the joint is supposed to rotate back to the original position before coming down, it would be something like:

    jointDef.upperAngle = atTop ? 0 : MathUtils.PI;
    jointDef.lowerAngle = atTop ? -MathUtils.PI : 0;
    

    I found answer from here