LibGdx物理引擎与帧率无关。

3
我正在开发一个类似于超级马里奥的简单平台游戏,使用Java和LibGdx引擎。我的问题是物理引擎与帧率无关。在我的游戏中,角色可以跳跃,跳跃高度明显与帧率相关。
在我的桌面上,游戏运行良好,每秒运行60帧。我还在平板电脑上尝试了游戏,它以较低的fps运行。结果是,在平板电脑上跳跃的角色比在桌面版本上跳得更高。
我已经阅读了一些有关修复时间步长的文章,我确实理解了其中的内容,但不足以应用到这种情况。我似乎缺少一些东西。
以下是代码中的物理部分:
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();
}

一个 jump() 函数被调用并添加了一个变量 jump_velocity = 40 到 velocity.y 中。

velocity 在碰撞检测中被使用。

1个回答

5
我认为你的问题出在这里:
velocity.add(0, world.getGravity());

您在修改速度时还需要缩放重力。请尝试以下操作:
velocity.add(0, world.getGravity() * deltaTime);

另外提一点,尝试使用box2D,它可以为你处理这些问题 :)

当然!现在它很好用。谢谢! 我不想使用Box2d,因为物理学相当简单,一个复杂的物理引擎并不是真正必要的。玩家角色几乎是唯一具有物理属性的东西。 - Arjan Frans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接