我是Libgdx的新手。我开发了一款带左右控制装置的汽车。一切都很好,但我想在钥匙释放后立即停车。显然很难阻止一辆快速行驶的汽车,但我仍然无法控制一下。我可以左右转,但我无法停止行驶的汽车。
这是我的代码
public class Car extends InputAdapter{
private Body chassis,leftWheel,rightWheel;
private WheelJoint leftAxis,rightAxis;
private float motorspeed=40;
public Car(World world, FixtureDef chassisFixtureDef, FixtureDef wheelFixtureDef,
float x, float y, float width, float height) {
//chassis
BodyDef bodydef=new BodyDef();
bodydef.type=BodyType.DynamicBody;
PolygonShape chassisShape=new PolygonShape();
chassisShape.setAsBox(width/2, height/2);
chassisFixtureDef.shape=chassisShape;
chassis=world.createBody(bodydef);
chassis.createFixture(chassisFixtureDef);
//left wheel
CircleShape wheelshape=new CircleShape();
wheelshape.setRadius(height/5f);
wheelFixtureDef.shape=wheelshape;
leftWheel=world.createBody(bodydef);
leftWheel.createFixture(wheelFixtureDef);
//right wheel
rightWheel=world.createBody(bodydef);
rightWheel.createFixture(wheelFixtureDef);
//left axis
WheelJointDef axisDef=new WheelJointDef();
axisDef.bodyA=chassis;
axisDef.bodyB=leftWheel;
axisDef.localAnchorA.set(-width/2 *0.75f + wheelshape.getRadius(),
-height/2*1.25f);
axisDef.localAxisA.set(0,1);
axisDef.maxMotorTorque=350;
leftAxis=(WheelJoint)world.createJoint(axisDef);
axisDef.bodyB=rightWheel;
axisDef.localAnchorA.x*=-1;
rightAxis=(WheelJoint)world.createJoint(axisDef);
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
switch(keycode) {
case Keys.D:
leftAxis.enableMotor(true);
leftAxis.setMotorSpeed(-motorspeed);
break;
case Keys.A:
leftAxis.enableMotor(true);
leftAxis.setMotorSpeed(motorspeed);
break;
}
return true;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
switch(keycode) {
case Keys.D:
leftAxis.enableMotor(false);
break;
case Keys.A:
leftAxis.enableMotor(false);
}
return true;
}
public Body getChassis() {
return chassis;
}
}
这是我的灯具定义
BodyDef grounddef=new BodyDef();
FixtureDef groundFixture=new FixtureDef();
FixtureDef wheelFixtureDef =new FixtureDef();
groundFixture.density=5;
groundFixture.friction=.4f;
groundFixture.restitution=.3f;
wheelFixtureDef.density=groundFixture.density*2.5f;
//cz car is gng higher wn startt so gvng wheel nerya density
wheelFixtureDef.friction=70;
wheelFixtureDef.restitution=.4f;
car=new Car(world,groundFixture,wheelFixtureDef, 1, 3, 6, 4);
Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter(){
@Override
public boolean scrolled(int amount) {
camera.zoom+=amount/25f;
return false;
}
},car));
我无法让车停下来。请帮助。谢谢提前
答案 0 :(得分:0)
首先,我认为您可能使用了错误的比例。 Box2D使用MKS(米,千克和秒)。你可能在这里使用像素而不是米。
chassisShape.setAsBox(width/2, height/2);
Box2D的FAQ建议使用
objects [that are] between 0.1 - 10 meters
否则你会遇到奇怪的情况。
但我不是主要问题。主要问题是你在keyDown处理程序中设置了运动速度,但在你的keyUp处理程序中你没有将它设置为零。因为如果释放钥匙,通过setMotorSpeed施加的力仍然存在,汽车将继续前进。要解决此问题,您可以将keyDown处理程序代码更改为:
switch(keycode) {
case Keys.D:
leftAxis.enableMotor(false);
leftAxis.setMotorSpeed(motorspeed);
break;
case Keys.A:
leftAxis.enableMotor(false);
leftAxis.setMotorSpeed(motorspeed);
}
如果这不能解决您的问题,那么我建议您发布您的enableMotor()方法。