gpt4 book ai didi

java - 独立于帧率的 LibGdx 物理

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:03 29 4
gpt4 key购买 nike

我正在开发一款简单的平台游戏,例如 super 马里奥。我将 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.add(0, world.getGravity());

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

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

单独说明一下,尝试使用 box2D,它可以为您处理这些问题:)

关于java - 独立于帧率的 LibGdx 物理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21609600/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com