# GML non-linear interpolation, or SLERP()

Discussion in 'Programming' started by Ephemeral, Dec 23, 2017.

Tags:
1. ### EphemeralMember

Joined:
Mar 1, 2017
Posts:
185
(I know, technically, it should be "serp" and not "slerp" but "slerp" is more fun to say.)

Anyway, after several hours of reading wikipedia and playing with graphing calculators, I'm admitting defeat and posing this problem to the community. I've drawn out a diagram to help visualize what I'm asking if you don't know what I mean by "standard easing curves".

The top left graph, shows the lerp function we already have, and the other three show the curvy variations of the three most common easing curves found in many programs. slerp() is of course sine interpolation which is useful across the board, glerp() is "gradual" interpolation, which is really good for audio, and flerp() is "fast" interpolation which is situationally useful also...

I was actually a little surprised to find that these functions were not already built in to GMS2 next to lerp() but I apparently don't remember enough math to figure these out myself, so:

Designing a slerp() function that works exactly like the lerp() function but curved. How math?

File size:
21.7 KB
Views:
66
2. ### FrostyCatMember

Joined:
Jun 26, 2016
Posts:
3,969
Look up the graph shapes on this page and find the formula you want.

3. ### TheSnidrHeavy metal viking dentistGMC Elder

Joined:
Jun 21, 2016
Posts:
451
That looks like the smoothstep function, which, I agree, should be a built-in function. Google smoothstep to find the equation
Slerp is something else, spherical linear interpolation between orientations, often used for quaternion interpolation!

Joined:
Mar 1, 2017
Posts:
211
5. ### EphemeralMember

Joined:
Mar 1, 2017
Posts:
185
Ah, I see, the way to do it isn't with sine functions at all. No wonder I was getting tripped up.

Alright, so, if I understand this correctly, GML's built-in lerp() works like so:
Code:
```/// @description linear interpolation
/// @param start
/// @param end
/// @param amount

var l_start = argument0;
var l_end = argument1;
var amount = argument2;

var result = (l_end * amount) + (l_start * (1 - amount));

return result;```
Yes?

Okay, if that is indeed the case, then a lerp_smooth() can be done the same way:
Code:
```/// @description smooth interpolation
/// @param start Real number
/// @param end Real number
/// @param amount Value between 0 and 1
/// @param smoother Boolean

var l_start = argument0;
var l_end = argument1;
var amount = argument2;
var smoother = argument3;

var s_amount;
if (smoother)
{
s_amount = (amount * amount * amount) * (amount * ((6 * amount) - 15) + 10);
}
else
{
s_amount = (amount * amount) * (3 - (2 * amount));
}
var result = (l_end * s_amount) + (l_start * (1 - s_amount));

return result;
```
I don't understand where the constants in that math come from, though. How did 3, 2, 6, 15, and 10 get derived? Why are the equations for smooth and smoother that different?

While I'm at it:
Code:
```/// @description ease in or out
/// @param start Real number.
/// @param end Real number.
/// @param amount
/// @param inverse Boolean.

var l_start = argument0;
var l_end = argument1;
var amount = argument2;
var inverse = argument3;

var e_amount;
if (!inverse)
{
e_amount = amount * amount;
}
else
{
e_amount = 1 - ((1 - amount) * (1 - amount));
}
var result = (l_end * e_amount) + (l_start * (1 - e_amount));

return result;
```

Last edited: Mar 15, 2019
6. ### EphemeralMember

Joined:
Mar 1, 2017
Posts:
185
Today I discovered that the formula I based the above scripts on is incorrect.

On the linked webpage, the formula has A and B swapped. (IE, lerp goes from 0 to 1, the formulas from that webpage go from 1 to 0)

I don't see people asking about smoothstep much, but just in case, I'll edit the scripts to correct this and make them copy-paste usable.