我正在尝试旋转通过旋转关节连接的身体,当空格键按钮按在我的桌面。它的工作很好,当我把所有的条件在创建,但不是按空格键工作。这是我的create方法
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
}
这里是hit函数
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();
}
我在你的Create方法中发现了一个错误,它应该像
revoluteJoint=(RevoluteJoint)world.createjoint(revolutejointDef);
并且在hit方法或者create方法中使用revoltejoint而不是revoltejointdef
revoluteJoint.enableMotor=true;