Java 独立于帧率的LibGdx物理

Java 独立于帧率的LibGdx物理,java,android,libgdx,game-physics,frame-rate,Java,Android,Libgdx,Game Physics,Frame Rate,我正在开发一个简单的平台游戏,比如超级马里奥。我在LibGdx引擎中使用Java。我有一个问题,物理是独立于帧率。在我的游戏中,角色可以跳跃,跳跃高度显然取决于帧率 在我的桌面上,游戏运行良好,每秒运行60帧。我也在平板电脑上尝试了这个游戏,它以较低的fps运行。发生的事情是,这个角色可以跳得比我在桌面版上跳得更高 我已经读过一些关于修复timestep的文章,我确实理解它,但还不足以将其应用于这种情况。我好像错过了什么 以下是代码的物理部分: protected void applyPhysi

我正在开发一个简单的平台游戏,比如超级马里奥。我在LibGdx引擎中使用Java。我有一个问题,物理是独立于帧率。在我的游戏中,角色可以跳跃,跳跃高度显然取决于帧率

在我的桌面上,游戏运行良好,每秒运行60帧。我也在平板电脑上尝试了这个游戏,它以较低的fps运行。发生的事情是,这个角色可以跳得比我在桌面版上跳得更高

我已经读过一些关于修复timestep的文章,我确实理解它,但还不足以将其应用于这种情况。我好像错过了什么

以下是代码的物理部分:

protected void applyPhysics(Rectangle rect) {
    float deltaTime = Gdx.graphics.getDeltaTime();
    if (deltaTime == 0) return;
    stateTime += deltaTime;

    velocity.add(0, world.getGravity());

    if (Math.abs(velocity.x) < 1) {
        velocity.x = 0;
        if (grounded && controlsEnabled) {
            state = State.Standing;
        }
    }

    velocity.scl(deltaTime); //1 multiply by delta time so we know how far we go in this frame

    if(collisionX(rect)) collisionXAction();
    rect.x = this.getX();
    collisionY(rect);

    this.setPosition(this.getX() + velocity.x, this.getY() +velocity.y); //2
    velocity.scl(1 / deltaTime); //3 unscale the velocity by the inverse delta time and set the latest position

    velocity.x *= damping;

    dieByFalling();
}
受保护的无效applyPhysics(矩形矩形){
float deltaTime=Gdx.graphics.getDeltaTime();
如果(deltaTime==0)返回;
stateTime+=deltaTime;
add(0,world.getGravity());
如果(数学绝对值(速度x)<1){
速度x=0;
if(接地和控制启用){
状态=状态。站立;
}
}
velocity.scl(deltaTime);//1乘以delta时间,这样我们就知道我们在这个帧中走了多远
if(collisionX(rect))collisionXAction();
rect.x=this.getX();
碰撞(rect);
this.setPosition(this.getX()+velocity.x,this.getY()+velocity.y);//2
velocity.scl(1/deltaTime);//3通过反增量时间取消速度缩放,并设置最新位置
速度x*=阻尼;
死亡坠落();
}
调用一个jump()函数,并将一个变量jump_velocity=40添加到velocity.y


速度用于碰撞检测。

我认为您的问题在于:

velocity.add(0, world.getGravity());
修改速度时,还需要缩放重力。尝试:

velocity.add(0, world.getGravity() * deltaTime);

另外,请尝试使用box2D,它可以为您处理这些问题:)

当然!现在很好用。非常感谢。我不想使用Box2d,因为物理非常简单,并不需要复杂的物理引擎。玩家角色几乎是唯一具有物理性的东西。