【发布时间】: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