• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How to calculate steps needed for lerp() to hit target value in step event?

Greetings,

I'll come in hard and true: I suck at mathematics.

I know that:

Code:
xx = lerp(xx, 10, 0.25);
Can also be written as:

Code:
xx = xx + ((10 - xx) * 0.25);
Let's say I give xx a starting value of 100 in a create event and I put the lerp() function above in the step event. How can I calculate how many steps it will take for xx to reach 10? Is there a formula that I can add in the create event so that I can predict in advance how many times the step event needs to run to for lerp() to reach the target value, in this case 10?

Knowing that would lead up to my ultimate goal: Would it also be possible to calculate how much xx has been accumulated during all the steps? Calculating the above function by hand the first step lerp() returns 77.5, the second 60.625, etc. I would like to know how much all the returns are combined when the target is reached.

I've been struggling a bit on how to go about this. I could just let the steps run and count the amount of steps and the accumulated xx... but that kind of defeats the point of such a nice function.

Cheers,

Tom
 
Last edited:

devKathy

Member
So 10 to 100 is a number line distance of 90, yeah?

We're taking steps of size .25 according to what I assume is your t parameter.

So if we want to increment by a number line distance of 1, we need to do this incrementation 4 times. How much is it for a distance of 90?

Edit: omg you wanted to go to a lower value. My bad
 
So 10 to 100 is a number line distance of 90, yeah?

We're taking steps of size .25 according to what I assume is your t parameter.

So if we want to increment by a number line distance of 1, we need to do this incrementation 4 times. How much is it for a distance of 90?

Edit: omg you wanted to go to a lower value. My bad
Also, that isn't correct. Following the 10 to 100 with a 0.25 interpolation, the first step would return 32.5, the second 49.375, the third 62.03125, the fourth 71.5234375, the fifth 78.642578125. Those are more than 4 increments. The easing makes it difficult to calculate. Do I understand that correctly? I believe lerp() rounds up a bit, so it is actually possible to reach the target.
 

devKathy

Member
Oh crap you're right. Step size isn't constant. Well there's a formula for the interpolant on wikipedia in 2 dimensions. Could no doubt be applied if you placed all points collinearly.

Rusty math, apologies.

I'll get the link.

https://en.m.wikipedia.org/wiki/Interpolation#Linear_interpolation

There's a section which says the function might be differentiable above that one.

Edit: nah was wrong again... not continuously differentiable.

Edit 2: so that's a way of saying. No it's not easy to predict exactly. Seems to be methods to get close though. E.g. finding a polynomial that's close.

https://en.m.wikipedia.org/wiki/Linear_interpolation#Linear_interpolation_as_approximation

Edit 3: fixed link.
 
Last edited:

Nidoking

Member
Let's call the entire distance to be moved (initial xx to 10) d. Then you're moving 0.25d in the first iteration, meaning that the distance remaining is 0.75d. In the second step, you move 0.25 * (0.75d), leaving a distance of (0.75^2) * d. It's a simple geometric sequence. The remaining distance after n iterations is (0.75^n) * d., so the distance traveled is d - (0.75^n) * d, or (1 - 0.75^n) * d.
This sequence approaches 0 in the limit, but will never actually equal 0. All you can do is determine how close to 0 you need the value to be before you consider it effectively reached. Let's say that a is so close to zero that when your distance is less than a, you consider yourself to have reached your target. So you want 0.75^n * d < a.
0.75^n < a / d (assuming d positive, which it is by definition)
n ln 0.75 < ln (a/d)
n > ln (a/d) / ln 0.75 (switching the direction of the inequality because ln 0.75 is negative)
So there you go.
 
Thanks devKathy and Nidoking! I'm slowly starting to get it, much appreciated. After a good night sleep I might go a different route altogether as I thought of another way to approach my issue. I'll keep playing with the formula you provided Nidoking! Thanks again.
 
Top