Search code examples
javaandroidlibgdxbox2drevolute-joints

How to set back and forth motion in revolute joint Libgdx?


I am trying to give back and forth motion of a body connected via revolute joint. I was trying to set reference to zero, when simulation starts 45 degree in clockwise then in anticlockwise back to reference and 45 degree anticlockwise from reference. i want Click the imgage,in which 0 is reference the 45 degre to 1,45 degree back i.e 2,then 45deg clockwise and then back to reference I tried this

RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
        revoluteJointDef.initialize(ballBody,rectBody,new Vector2(25,30));
        revoluteJointDef.lowerAngle=0.785f;
        revoluteJointDef.upperAngle=0.785f;
        revoluteJointDef.localAnchorA.set(25,30);
        revoluteJointDef.enableMotor=true;
        revoluteJointDef.enableLimit=true;
        revoluteJointDef.maxMotorTorque=1000f;
        revoluteJointDef.motorSpeed=12.6f;
        revoluteJointDef.referenceAngle=0f;

Solution

  • you can get back and forth motion by giving motor speed in two direction let's say you first want move in -45degree you can do this by

    revolutejoint.setMotorSpeed=-12f;
    

    remeber first set the limit and use above example,then you can ask for current angle of joint and based on that you can reverse the motor speed like

    if(revoluteJoint.getAngle<=45)
    revolutejoint.setMotorSpeed=12f;
    

    always use radian for angle, which i haven't shown. if someone know better than this i am glad to know.