Higher framerate causes higher jump height

Hi all!

Please find a link here to a playground: https://www.babylonjs-playground.com/#3EDS3A#22

Please click inside the scene to activate pointer lock and use the WASD keys to move and the SPACE key to jump.

In Chrome, with 60 FPS, the character can only jump a short height:

In Brave, with ~300 FPS (using --disable-frame-rate-limit), the character can jump over the top of the red slide:

Note: The GIF’s above show lower FPS than mentioned, since the recording software decreases FPS.

Could someone please shed light on why a higher framerate allows the character to jump higher?

Code details:
The function move() is called every frame in scene.registerBeforeRender(). Inside the function move(), the delta time between both frames is considered:

const delta = command.frameTime - prevFrameTime;

Whenever the player jumps, their vertical velocity is suddenly increased to velocity.y = 0.15. And every frame, the vertical velocity is decreased by delta / 3000 to simulate gravity, which takes into account the delta time between both frames.

Since we are accounting for the delta time, I’m unsure why the jump height depends on the framerate.

Thank you all for your help! :smiley:

You accounted for delta when applying changing velocity, but didn’t do so when changing position. Your velocity.y should be scaled by delta before applying it to TransformCoordinates.


Thank you for your response, @Cort!

The xyz components of velocity were all scaled by delta before applying it to TransformCoordinates.

... velocity.z = direction.z * delta / 300;
... velocity.x = direction.x * delta / 300;
velocity.y -= delta / 3000;
const rotationAxis = BABYLON.Matrix.RotationAxis(BABYLON.Axis.Y, viewAngleY);
const rotatedVelocity = BABYLON.Vector3.TransformCoordinates(velocity, rotationAxis);

I may be misunderstanding your point and would be grateful if you could elaborate or please help in showing what changes should be added to the Playground. Thank you!

@Cort, you were right!

After changing the line to

const rotatedVelocity = BABYLON.Vector3.TransformCoordinates(velocity.multiplyByFloats(1, delta/10, 1), rotationAxis);

it seems to work now! Ah I think I understand my mistake now. Thank you so much, Cort :smiley:

1 Like