Framerate independent lerp

Kristian Kristola

Kristian Kristola

· 5 min read
flutter

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:

Graph of the formula

It takes about 19 iterations to reach 50.

 


 

Let's try with a twice higher delta time

delta_time = 0.033334 (30 fps)

Graph of the formula

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) 

Graph of the formula

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.

Kristian Kristola

About Kristian Kristola

I'm a consultant and software developer with a strong foundation in game programming and the ability to adapt to new technologies and languages. Please feel free to contact me at:@
Copyright © 2024 Kiuas Games. All rights reserved.
Made by Osuuskunta Kiuas Games
Powered by Vercel

This website uses Google Analytics to collect anonymous information such as the number of visitors to different pages.