How to make a revoluteJoint oscillate?

江枫思渺然 提交于 2019-12-12 05:14:50

问题


I have created a revoluteJoint with a motor and lower & upper limits. I would like to know how to make the revoluteJoint oscillate between the lower & upper limits. Currently the body swings from the lower to the upper limit and then stops. Any help will be appreciated. This is the code

   final RevoluteJointDef revoluteJointDef2 = new RevoluteJointDef();   
   revoluteJointDef2.initialize(legBody, circleBody1, circleBody1.getWorldCenter());
   revoluteJointDef2.enableMotor = true;
   revoluteJointDef2.enableLimit = true;
   rj2 = (RevoluteJoint) this.mPhysicsWorld.createJoint(revoluteJointDef2);
   rj2.setMotorSpeed(2);
   rj2.setMaxMotorTorque(10);
   rj2.setLimits((float)(30 * (Math.PI)/180), (float)(270 * (Math.PI)/180));

How do I make the joint reverse direction and repeat again?


回答1:


Do at every step:

if ((joint->GetUpperLimit() - joint->GetJointAngle()) < yourSomeAlpha)
{
     // Reached upper limit, go to another limit
     joint->SetMotorSpeed(-2);
}
if ((joint->GetJointAngle() - joint->GetLowerLimit()) < yourSomeAlpha)
{
     // Reached lower limit, go to another limit
     joint->SetMotorSpeed(2);
}

Parameter yourSomeAlpha choose experimental. Try, for example, 0.01.




回答2:


I was able to create an oscilatting revoluteJoint by implementing a timer task. Every time the timer fires I reverse the motor speed and make the revoluteJoint oscillate between lower & upper limits.

    class RemindTask extends TimerTask {
       RevoluteJoint rj1;;
        RemindTask(RevoluteJoint rj){
        rj1 = rj;
    }
    @Override
    public void run() {
        Log.d("x","x" +"Reversing motor");
        reverseMotor();          
    }

    public void reverseMotor(){
        rj1.setMotorSpeed(-(rj1.getMotorSpeed()));
        rj1.setMaxMotorTorque(100);

    }
}

For a link to implementation and code please take a look at my blog post http://gigadom.wordpress.com/2013/07/02/simulating-an-oscillating-revolutejoint-in-android/



来源:https://stackoverflow.com/questions/17408019/how-to-make-a-revolutejoint-oscillate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!