Java 如何使相机与播放器的速度相同

Java 如何使相机与播放器的速度相同,java,libgdx,box2d,Java,Libgdx,Box2d,我正在用Box2d在LibGDX上制作一个无止境的跑步者。我想让相机和播放器以相同的速度移动,这样当一切正常时,播放器就在屏幕的中央。我不想总是让球员成为中锋,因为如果球员被卡住,我想让球员被摄像机甩在后面(比如在板条箱后面)。有了它,我想我可以用与玩家相同的默认速度来平移相机。但是不起作用。帮忙 render()(使用相机更新): 玩家的一部分: public Player(Texture texture, float x, float y, World world) { this.t

我正在用Box2d在LibGDX上制作一个无止境的跑步者。我想让相机和播放器以相同的速度移动,这样当一切正常时,播放器就在屏幕的中央。我不想总是让球员成为中锋,因为如果球员被卡住,我想让球员被摄像机甩在后面(比如在板条箱后面)。有了它,我想我可以用与玩家相同的默认速度来平移相机。但是不起作用。帮忙

render()(使用相机更新):

玩家的一部分:

public Player(Texture texture, float x, float y, World world) {
    this.texture = texture;
    setWidth(texture.getWidth()); //pixels
    setHeight(texture.getHeight()); //pixels
    setPosition((x + getWidth()/2), y + (getHeight()/2)); //pixels
    setName("player");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(x / Constants.PIXELS_TO_BOX, y / Constants.PIXELS_TO_BOX); //meters

    body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(texture.getWidth()/2 / Constants.PIXELS_TO_BOX , texture.getHeight()/2 / Constants.PIXELS_TO_BOX); //meters

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    body.createFixture(fixtureDef);
    body.setFixedRotation(true);
    shape.dispose();    

    body.setUserData(this);
}

@Override
public void act(float delta) {
    body.setLinearVelocity(new Vector2(Constants.DEFAULT_VELOCITY, body.getLinearVelocity().y));
    //what I use to set velocity
    if (hasJumped) {
        jump();
        hasJumped = false;
        Gdx.app.log("player", "jumped");
    }
}

你需要将速度乘以delta时间来得到平移。在这种情况下,我修复相机并移动世界。
public Player(Texture texture, float x, float y, World world) {
    this.texture = texture;
    setWidth(texture.getWidth()); //pixels
    setHeight(texture.getHeight()); //pixels
    setPosition((x + getWidth()/2), y + (getHeight()/2)); //pixels
    setName("player");

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(x / Constants.PIXELS_TO_BOX, y / Constants.PIXELS_TO_BOX); //meters

    body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(texture.getWidth()/2 / Constants.PIXELS_TO_BOX , texture.getHeight()/2 / Constants.PIXELS_TO_BOX); //meters

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    body.createFixture(fixtureDef);
    body.setFixedRotation(true);
    shape.dispose();    

    body.setUserData(this);
}

@Override
public void act(float delta) {
    body.setLinearVelocity(new Vector2(Constants.DEFAULT_VELOCITY, body.getLinearVelocity().y));
    //what I use to set velocity
    if (hasJumped) {
        jump();
        hasJumped = false;
        Gdx.app.log("player", "jumped");
    }
}