【问题标题】:LibGdx physics independent from framerateLibGdx 物理独立于帧率
【发布时间】:2014-03-03 19:02:48
【问题描述】:

我正在开发像超级马里奥这样的简单平台游戏。我正在使用带有 LibGdx 引擎的 Java。我对物理独立于帧率有疑问。在我的游戏中,角色可以跳跃,跳跃高度显然取决于帧率。

在我的桌面上,游戏运行良好,每秒运行 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。

速度用于碰撞检测。

【问题讨论】:

    标签: java android libgdx game-physics frame-rate


    【解决方案1】:

    我认为你的问题在这里:

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

    您还需要在修改速度时缩放重力。试试:

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

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

    【讨论】:

    • 当然!它现在工作正常。谢谢!我不想使用 Box2d,因为物理非常简单,不需要复杂的物理引擎。玩家角色几乎是唯一具有物理特性的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多