# lerp Clarification

Discussion and feedback on Construct 2

### » Sun Jan 29, 2012 12:47 pm

I don't fully understand the way lerp works.

I'm doing this. Set size x = lerp(self.X,0,0.5) to get an object to disappear in time.

Now, if the object is big this works, otherwise it's almost the same as setting it to zero.

Now, for example, lerp(40,0,0.5) means that every tick the size goes from 40 to 0 in steps of (40*0.5=20) So basically two ticks to go from 40 to 0?

Now a tick is not completely reliable, the way I understand If I have 1000 events a tick will last more than say 2 events, am I correct? Is there any way to control this, for instance, do each step every X seconds?

Thank you
B
28
S
9
G
6
Posts: 525
Reputation: 8,269

### » Sun Jan 29, 2012 1:19 pm

I'm just wondering the same thing, wouldn't it be:
lerp(A,B,0.5*60*dt)

I usually just do "*60*dt" in an equation if I forgot to add it previously to something. Though the 60 could be exchanged to whatever framerate you're use to seeing.alspal2012-01-29 13:20:38
B
126
S
63
G
16
Posts: 1,763
Reputation: 18,772

### » Sun Jan 29, 2012 1:24 pm

Not exactly. It uses the last part as a percentage.
So for lerp(self.x,0,0.5), in the first tick it would be 50% of the way between it's own x and zero. then the next tick it would do the same, but since it is now closer, it only goes half the distance this tick. Then the next tick it goes half the distance, etc, etc.

So then, in reality it will never reach its destination, although it will get awfully close (and rounding errors will see it get there anyway).

By this logic, lerp(40,0,0.5) will always read 20. because 40 is a constant and not allowed to change. the first value should pretty much always be a variable.

By the way,
instead of lerping by like 0.5, it is best practice to lerp by dt * K, where K is a constant (higher = faster lerping.

Hope I helped a bit.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

### » Tue Jan 31, 2012 2:25 pm

It's still a mystery to me.
I'm trying to make an object increase it's size from 0 to 600 in an x amount of time.

System create object at (center of the screen)
set object size 0,0
set object size lerp(object.width,600,0.1*dt)

The object doesn't even show, if I put 0.5 it goes from 0 to 300 in a fraction of a second and stops..

Why? I don't understand this at all..
B
28
S
9
G
6
Posts: 525
Reputation: 8,269

### » Tue Jan 31, 2012 7:42 pm

The lerp expression take the input range made by the two first values and consider it as equivalent to 1, then returns the value that corresponds to the x ratio. In other words, it takes the difference between the first two values, multiply it by the interpolation ratio and adds the result to the first value.

This is what happens when you set a variable to lerp(variable,600,0.5) every tick, considering that the variable initial value is zero:

In the first tick the variable will be set to the middle point between the range 0-600, which is 300. In the next tick the lerp will take the current variable value that is 300, and return the middle point of the range 300-600, which is 450. In the third tick the range will be 450-600 and lerp will return the middle point 525, and so on for the other ticks until it reaches 600.

The rate at which the variable changes is a curve and not a straight line, so you get a deceleration as the variable approaches the destination value.

If you want a constant rate you need to set constant values to the range and increment the ratio every tick. So you need to do something like:

+Every Tick
>set width to lerp(0,600,variable)
>set variable to clamp( variable + dt / duration ,0,1)

Being 'duration' the desired time in seconds to go from 0 to 600. Which means that if duration=2 the width of the object will go from 0 to 600 in 2 seconds.

The clamp expression is used to ensure that the variable value will always stay between 0 and 1, otherwise the width of the object would increase forever.Animmaniac2012-01-31 19:51:19
Scirra Employee
B
124
S
45
G
15
Posts: 704
Reputation: 15,286

### » Wed Feb 01, 2012 5:17 pm

Thank you, really thank you, I finally understand, you should make a tutorial with this information, otherwise will be lost in time (like tears in the rain )
B
28
S
9
G
6
Posts: 525
Reputation: 8,269

### » Wed Feb 01, 2012 6:25 pm

@Animmaniac - yes, definitely tutorial material. Superb graphics - they deserve a wider audience.
B
105
S
20
G
12
Posts: 549
Reputation: 20,292

### » Wed Feb 01, 2012 8:52 pm

Yep, this would make a great tutorial.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

### » Wed Feb 01, 2012 10:46 pm

here's a little tutorial (including an interactive exe) on lerp

lerp tut

it also contains a link to an interactive exe with an example usage of lerp, qarp, and cubic.lucid2012-02-01 22:47:37
Spriter Dev
B
84
S
20
G
12
Posts: 3,224
Reputation: 16,254

### » Fri Mar 02, 2012 8:59 pm

How do i...
I have sprite, when i press and hold for example 'q' my sprite smoothly increased two times but no more?
B
91
S
18
G
34
Posts: 1,835
Reputation: 27,804

Next

### Who is online

Users browsing this forum: gameglaux, Jeerdus, qwer123456 and 10 guests