如何在Libgdx中使用汽车刹车

3

我是一个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));

我无法让汽车停下来,请帮忙。谢谢。

1个回答

0
首先,我认为你可能使用了错误的度量单位。Box2D 使用MKS(米,千克和秒)。你可能在这里使用了像素而不是米。
chassisShape.setAsBox(width/2, height/2);

Box2D的常见问题解答建议使用

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()方法。
请参见https://code.google.com/p/box2d/wiki/FAQ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接