I want to achieve a platformer jump similar to that of Super Mario Bros. (keep pressed the jump button to jump higher) and I want it to be frame rate independent. At 30 FPS it works as I want. However, I noticed that, if frame rate is lower (e.g. 10 FPS) the jump height is much lower.
The following is the pseudo-code I am using on my character. What am I doing wrong? Note: I'm considering the Y-axis to grow downwards, so a negative vertical speed means the character is moving upwards.
jump_force = 4000;
jump_max_speed = 800;
gravity = 2000;
yspeed = 0; // vertical speed (negative is upwards)
is_jumping = false;
update() { // called every frame
dy = 0; // delta y-position for this frame
if (yspeed != 0 && is_grounded()) {
yspeed = 0;
}
if (jump_button_pressed() && is_grounded())
is_jumping = true;
else if (is_jumping && !jump_button_pressed())
is_jumping = false;
if (is_jumping) {
yspeed -= jump_force * dt(); // dt() is delta time, i.e. time elapsed since last frame
// cap vertical speed to limit jump height
if (yspeed < -jump_max_speed) {
is_jumping = false;
yspeed = -jump_max_speed;
}
}
else if (!is_grounded()) {
// apply gravity only when the character is not jumping
yspeed += gravity * dt();
}
dy = yspeed * dt();
// if character would fall inside terrain, place it just above it
if (dy > 0 && check_for_ground(x, y + dy)) {
y = ground.y;
yspeed = 0;
}
else
y += dy;
}
dt()
function might not work correctly? \$\endgroup\$