【问题标题】:Why does wheel movement in Box2d doesn't cause car to move?为什么 Box2d 中的车轮移动不会导致汽车移动?
【发布时间】:2015-01-23 10:40:25
【问题描述】:

我正在为我的 Android 游戏 (Java + LibGDX) 开发基于 Box 2d 物理的​​移动车辆。当我认为我的一切都已启动并运行时,我的带有两个轮子的汽车站在地上,但是当我转动轮子时,汽车却不动。

这是我的游戏课:

public class MyGdxGame implements ApplicationListener {
World world = new World(new Vector2(0, -10), true);

Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;

static final float BOX_STEP=1/60f;
static final int BOX_VELOCITY_ITERATIONS=6;
static final int BOX_POSITION_ITERATIONS=2;

static final int WIDTH=256;
static final int HEIGHT=169;

Hero hero;
MovableBox box;



@Override
public void create() {
    camera = new OrthographicCamera();
    camera.viewportHeight = HEIGHT;
    camera.viewportWidth = WIDTH;
    camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
    camera.update();

   Vector2[] anArray;
   anArray = new Vector2[4];

    anArray[0]= new Vector2(-30,0);
    anArray[1]= new Vector2(55,55);
    anArray[2]= new Vector2(110,55);
    anArray[3]= new Vector2(195,0);

    //Ground body
    BodyDef groundBodyDef =new BodyDef();
    groundBodyDef.position.set(new Vector2(-8-13-20-20-10, -44));
    Body groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.set(anArray);
    //groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
    groundBody.createFixture(groundBox, 0.0f);

    //Ground body
    BodyDef groundBodyDef2 =new BodyDef();

    groundBodyDef2.position.set(new Vector2(13*11-4-7-8-13-20-20-10, -44));
    Body groundBody2 = world.createBody(groundBodyDef2);
    PolygonShape groundBox2 = new PolygonShape();
    groundBox2.set(anArray);
    //groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
    groundBody2.createFixture(groundBox2, 0.0f);

    hero = new Hero();
    hero.create(world, 0, 0);

    box.create(world, WIDTH / 4 * 3, HEIGHT / 2);
    boxes.add(box);

    debugRenderer = new Box2DDebugRenderer();
}
private void update(){
    hero.update();

    for(MovableBox b : boxes) {
        b.update();
    }
}
@Override
public void dispose() {
}
@Override
public void render() {
    update();
    camera.position.set(hero.getPos(), 0f);
    camera.update();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    debugRenderer.render(world, camera.combined);
    world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}

还有Hero类(其实应该叫车)

    Body body;
Body wheel, wheel2;
public void create(World world, int posx, int posy){

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(posx, posy);
    body = world.createBody(bodyDef);
    PolygonShape dynamicCircle = new PolygonShape();
    dynamicCircle.setAsBox(8, 3);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicCircle;
    fixtureDef.density = 4.0f;
    fixtureDef.restitution = 0.1f;
    fixtureDef.friction=0.95f;
    body.createFixture(fixtureDef);

    BodyDef bodyDef2 = new BodyDef();
    bodyDef2.type = BodyDef.BodyType.DynamicBody;
    wheel = world.createBody(bodyDef2);
    CircleShape dynamicCircle2 = new CircleShape();
    dynamicCircle2.setRadius(2);
    FixtureDef fixtureDef2 = new FixtureDef();
    fixtureDef2.shape = dynamicCircle2;
    fixtureDef2.density = 4;
    fixtureDef2.friction=3.0f;
    fixtureDef2.restitution=0.1f;
    wheel.createFixture(fixtureDef2);

    wheel2 = world.createBody(bodyDef2);
    wheel2.createFixture(fixtureDef2);


    RevoluteJointDef revoluteJointDef  = new RevoluteJointDef();
    revoluteJointDef.bodyA = body;
    revoluteJointDef.bodyB = wheel;
    revoluteJointDef.localAnchorA.add(-8,-6);
    world.createJoint(revoluteJointDef);

    RevoluteJointDef revoluteJointDef2  = new RevoluteJointDef();
    revoluteJointDef2.bodyA = body;
    revoluteJointDef2.bodyB = wheel2;
    revoluteJointDef2.localAnchorA.add(8,-6);
    world.createJoint(revoluteJointDef2);

}
public Vector2 getPos(){
    return body.getPosition();
}
public void update(){
    if(Gdx.input.isTouched())
    {
        if(Gdx.input.getX()<600){
            wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()+0.1f);
            wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y, wheel2. getAngle() + 0.1f);
        }
        else{
            wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()-0.1f);
            wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y,wheel2.getAngle()-0.1f);

        }


    }
}

【问题讨论】:

    标签: android libgdx box2d game-physics revolute-joints


    【解决方案1】:

    嗯,我认为您需要考虑两件事。

    首先我认为,如果您希望您的物理引擎“强制”汽车在车轮旋转时移动,您可能应该查看BodyDef阻尼 参数(linearDamping 和@ 987654323@)。为地面和汽车车轮设置这些可能会帮助您在车轮打滑时移动汽车。

    第二件事是你真的需要分析它是否是正确的方法。也许您应该考虑通过移动车身本身并旋转车轮来模拟汽车的运动。我认为这种方法应该可以让您更好地控制汽车的运动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2011-01-04
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多