【问题标题】:How to control Revolute joint using input button?如何使用输入按钮控制旋转关节?
【发布时间】:2016-09-09 12:17:10
【问题描述】:

我正在尝试通过旋转关节旋转身体连接,当在我的桌面上按下空格键按钮时。当我将所有条件都放在创建中时它工作正常,但按空格键按钮不工作。 这是我的创建方法

            ballBody =createBall();//first body to the joint
            rectBody=createRect();//second body to the joint

            revoluteJointDef = new RevoluteJointDef();
            revoluteJointDef.initialize(ballBody,rectBody,new Vector2(0,0));
            revoluteJointDef.lowerAngle=0;
            revoluteJointDef.upperAngle=0.785f;
            revoluteJointDef.localAnchorA.set(2,0);
            revoluteJointDef.localAnchorB.set(0,6);
            revoluteJointDef.collideConnected=false;

            revoluteJointDef.lowerAngle=-0.734f;
            revoluteJointDef.upperAngle=0.735f;
            revoluteJointDef.maxMotorTorque=100f;
            revoluteJointDef.motorSpeed=-12.6f;
            revoluteJointDef.referenceAngle=0f;
            revoluteJointDef.enableLimit=true;
            joint = world.createJoint(revoluteJointDef);

我在按下空格键后启用电机

  public boolean keyDown(int keycode) {
        if(keycode== Input.Keys.SPACE) {
                   hit();  // call to hit function
        }

这里是命中函数

  private void hit(){
        revoluteJointDef.enableMotor=true;
        }

而我的渲染方法是这样的

 public void render(float delta) {
        Gdx.gl.glClearColor(0.5f, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        game.batch.setProjectionMatrix(camera.combined);
        world.step(1f/60f, 6, 2);
        debugRenderer.render(world, camera.combined);
        game.batch.begin();
         game.batch.end();
    }

如有任何建议,请帮助我。

【问题讨论】:

    标签: libgdx box2d revolute-joints


    【解决方案1】:

    我在你的 Create 方法最后一行发现错误,应该是这样的

    revoluteJoint=(RevoluteJoint)world.createjoint(revolutejointDef);
    

    在 hit 方法或 create 方法中使用 revolutejoint 代替 revolutejointDef

    revoluteJoint.enableMotor=true;
    

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2014-12-30
      • 2018-01-24
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多