How is lerp meant to work with weight?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’ve done some reading on lerp around the webs and understand that you have two values and a weight.

I believe the idea is that the weight is meant to help “blend” between the two values.

So if I have lerp(1,3,.5) it should produce 2?

I have a situation from another thread where I am doing as such:

if (lerp(bat.get_pos().y, bat.get_pos().y-closeEnough, 50*delta) >= mousePos.y):
    #align bat to mouse code here

I was playing around and I don’t fully understand the weight aspect obviously. I simply copied the X*delta from another script then adjusted the value until it worked.

I started at 1 * delta and jitter occured in the bat when it reached the mouse. Raised and raised it until 50*delta where it seems happy and stable.

Can anyone explain what’s happening there to help solidify this in my mind? Any help much appreciated as I’m glad it’s working but I want to know WHY it’s working.

:bust_in_silhouette: Reply From: avencherus

You can think of the “weight” also as a time value or a percentage.

So in your example you have 1 and 3. At time 0 it’s going to be fully 1 and at time 1 it’s going to be fully 3. From 0 to 1 it’s going to blend in a “linear” way between the two numbers (in perfectly divided steps). You can plot these steps and you’ll get a line, which is where it gets the name.

What it does is maps the space between the two numbers onto a range from 0 to 1, or 0% to 100%.

Back to the (1,3,.5) example. At .5, you’re at 50%, which the number halfway between 1 and 3 is 2.

It’s easier I think to just pick some values, and do a loop that iterates from 0 to 1 in small steps (.1 or .05), and print out the results. I find that to be the best way to see what an interpolation is doing.

Perfect thank you. I’ll try the plot when at the PC. That’s really appreciate.

Re the delta*50 in my practical example, do you have any idea why such a large number? Also is delta required? I guess delta is a time since last frame, (as I understand it). I’m a bit muddled by the whole thing.

Robster | 2017-02-10 21:30

Not exactly sure, the context is not clear to me, but delta * 50 is not that large.

A delta is typically 1 / FPS, so if it’s 60, that’s 16ms or .0166666666 and so on. Times 50 it comes out like .833333. So that’s like 83% of the distance between any two numbers.

You may want to go learn more about fixed time steps and how game loops utilize them. Delta is something of a scalar for a slice of time time. It will chop up the values that will add up to one second of game time, based on the frame rate you’ve decided.

With lerping for animation, tweening, easing, whatever you like to call it, your t value is like a knob that you control. So how much time it takes to go from 0 to 1 or if you use the full range is up to you. It’s something only you as the creator can decide.

The values and time are up to what you think looks best. And there is are other methods that use curves instead of a linear progression. These can appear more organic, or interesting.

If you need to play around with that there is a decent site for applying various interpolations to X,Y cursor movement at this site: Easing Equations

Though it’s best to just dump out the values and comb over the actual steps one by one to best understand. I recommend also using ending numbers like 1, 10, or 100, so your results can also be viewed as percentages.

avencherus | 2017-02-10 22:12

Thank you, this is really helpful. I’m off to lerp about for a bit to get a visual grasp on this with some output tests. Really appreciate such a thorough description.

Robster | 2017-02-13 07:54