Handmade Hero » Forums » Code » Frame independence and gravity
david009
11 posts
#10952 Frame independence and gravity
4 months ago

How do you get gravity to work with frame independence? I multiply both gravity and jump speed with delta time and when fps change from 30 to 60 I get a 50% shorter jump, since jump vector gets exponentially shorter with gravity applied every frame, and at 60 fps you come out with half the length of a jump than at 30 fps, both taking the same amount of time.
How do you solve this?
mmozeiko
Mārtiņš Možeiko
1349 posts
1 project
#10953 Frame independence and gravity
4 months ago Edited by Mārtiņš Možeiko on Feb. 20, 2017, 8:12 p.m.

Day 43 covers this: https://asafgartner.github.io/annotations_player/hmh.html#day043#833

In simplest case you do :
Snext = Sprev + dt*Vprev + 0.5*dt*dt*A
Vnext = Vprev + dt*A

where S is position, V is velocity and A is acceleration (gravity in your case) constant/vector and dt is delta time between frames.
david009
11 posts
#10956 Frame independence and gravity
4 months ago

Thanks a lot :)