gpt4 book ai didi

java - 如何在 Libgdx 中对汽车施加制动

转载 作者:行者123 更新时间:2023-12-01 22:53:16 25 4
gpt4 key购买 nike

我是 Libgdx 的新手。我开发了一辆带有左右控制装置的汽车。一切都很好,但我想一松开 key 就停下车。显然很难停下一辆快速行驶的汽车,但我仍然无法控制。我可以向左或向右走,但我无法阻止行驶中的汽车。

这是我的代码

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

我无法让车停下来。请帮忙。提前致谢

最佳答案

首先,我认为您可能使用了错误的比例。 Box2D 使用 MKS(米、千克和秒)。您可能在这里使用了像素而不是米。

chassisShape.setAsBox(width/2, height/2);

Box2D的FAQ建议使用

objects [that are] between 0.1 - 10 meters

否则你可能会遇到奇怪的情况。

但这不是主要问题。主要问题是您在 keyDown 处理程序中设置了电机速度,但在 keyUp 处理程序中没有将其设置为零。因为如果您松开 key ,通过 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

关于java - 如何在 Libgdx 中对汽车施加制动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com