How do I understand the lerp function

Just started using Construct 2? Post your questions here

Post » Tue Jan 26, 2016 8:30 am

Dear all.
Could you explain the purpose of the lerp function ?

Thanks
B
3
Posts: 6
Reputation: 187

Post » Tue Jan 26, 2016 10:59 am

Using the search function with the words "lerp explanation" this is one one of the posts I found..
You can try the search function yourself and find even more..

Yann wrote:Lerp is a very simple function
Lerp stands for Linear intERPolation

This function need 3 numbers parameter
lerp(a,b,t)

a and b are whatever number you want
but t goes from 0 to 1 (well it can go under and over but... let's keep things simple)

lerp(a,b,0) returns a
learp(a,b,1) returns b

the values in between 0 and 1 returns a number X between a and b such as X-a = (b-a)*t
so t describe more or less where you are in between a and b

lerp(a,b,0.5) return the middle so a+(b-a)/2

lerp(0,10,0) = 0
lerp(0,10,1) = 10
lerp(0,10,0.5) = 5

the way newt use it with
Code: Select all
sprite set X to lerp(self.X,destination,0.5*dt)
is a little trick
the .X you have in the lerp is the .X you change in the expression
and 0.5*dt is always more or less the same (depending on fps)

so from step to step in the movement, destination-self.X will be shorter and shorter, so the lerp(self.X,destination,0.5*dt) will continue to move the sprite but slower and slower (ease out)

In short the only issue with this technique is that the sprite will takes time to exactly get to destination. Mathematically it should never arrive but thanks to float rounding stuff it will.
Anyway you should not rely too much on
if sprite.X = destination

that's all :D
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jan 26, 2016 11:55 am

Thank's that clear now.
B
3
Posts: 6
Reputation: 187


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests