• 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!

GML non-linear interpolation, or SLERP()

E

Ephemeral

Guest
(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?
 

Attachments

TheSnidr

Heavy metal viking dentist
GMC Elder
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!
 
E

Ephemeral

Guest
Look up the graph shapes on this page and find the formula you want.
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 by a moderator:
E

Ephemeral

Guest
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.
 
Top