我正在开发像超级马里奥这样的简单平台游戏.我正在使用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 …Run Code Online (Sandbox Code Playgroud)