Photograph by Ambrose Chua on Unsplash
Are you using linear interpolation, lerp
, to make a variable change smoothly over time towards a target value?
And looking for the formula to do so in a framerate independent manner? So that the change happens at the same speed,
regardless of the current framerate.
Here it is:
new_value = lerp(previous_value, target_value, 1.0 - pow(constant, delta_time));
Let's see if it works. Let's set:
initial value = 0
target value = 100
constant = 0.1
delta_time = 0.016667 (60 fps)
And see how many iterations will it take to reach 50:
It takes about 19 iterations to reach 50.
Let's try with a twice higher delta time
delta_time = 0.033334 (30 fps)
Now it takes about 10 iterations to reach 50, which is excpected as it's about half of the previous amount of iterations. The app is running at half the framerate, so it should get there in half the iterations.
Let's try with a lower delta time
delta_time = 0.008333 (120 fps)
Now it takes about 38 iterations to reach 50, which is correct as the computer will have time to do twice as many iterations as in the first example.