【问题标题】:How to apply brake in car in Libgdx如何在 Libgdx 中应用汽车刹车
【发布时间】:2015-02-25 06:21:16
【问题描述】:

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

我无法让车停下。请帮助。在此先感谢

【问题讨论】:

    标签: java libgdx box2d


    【解决方案1】:

    首先,我认为您可能使用了错误的比例。 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() 方法。

    https://code.google.com/p/box2d/wiki/FAQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多