I am working on a simple platform games like Super Mario. I am using Java with the LibGdx engine. I have a problem with physics being independent of the framerate. In my game the character can do a jump, the jump height is apparently dependent of the framerate.
On my desktop the game runs fine, it runs at 60 frames per second. I also tried the game on a tablet on which it ran at a lower fps. What happened was that the character could jump much higher than when I jumped on the desktop version.
I have already read some articles about fixing the timestep, I do understand it, but not enough to apply it to this situation. I just seem to be missing something.
Here is the physics part of the code:
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();
}
A jump() function gets called and adds a variable jump_velocity = 40 to the velocity.y.
The velocity is used in the collision detection.