怎么做一个revoluteJoint振荡?

时间:2013-07-01 15:19:28

标签: android box2d

我创造了一个带电机和低速电机的旋转接头。上限。我想知道如何使revoluteJoint在低位和低位之间振荡。上限。目前,身体从下限摆动到上限然后停止。任何帮助将不胜感激。这是代码

   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));

如何制作关节反方向并重复?

2 个答案:

答案 0 :(得分:0)

每一步都做:

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);
}

参数yourSomeAlpha选择实验。试试,例如,0.01。

答案 1 :(得分:0)

我能够通过实现计时器任务来创建一个振荡的revoluteJoint。每次定时器点火时,我都会反转电机速度,使旋转接头在低速和低速之间振荡。上限。

    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);

    }
}

有关实施和代码的链接,请查看我的博文http://gigadom.wordpress.com/2013/07/02/simulating-an-oscillating-revolutejoint-in-android/