Maths and easing functions!

i know that the lua api has plenty already, but i feel pretty compelled to give a shout out to the wonder that is maths and easing functions in games, not only for animating things but also calculating really anything!

i'll give some examples of what i mean in a moment, but real quick i just wanted to point out to those who don't know about it already this incredible website which has loads of wonderful easing functions (something that is definitely useful if you use pure c) that can help you do things like i'm about to explain.

so, obviously using this kinda stuff is super useful for moving entities around a screen or driving animations, but one thing i've always loved using easing functions for is difficulty curves. take for example this code i've written where i do the following:

const int min = 0;
float t = inverse_lerp(min, max, invaders_killed);
speed = ease_in_quart(MIN_SPEED, MAX_SPEED, t);

doing this has allowed me to increase the speed of the invaders in my game based on how many have been killed, and will do so along an ease in quart curve. or how about this bit of code where i do this:

const float min = 300.f;
const float max = 1000.f;
const float freq = remap(0.f, 100.f, min, max, percent);
snd->synth->playNote(reload_sound, freq, 1.f, 0.1f, 0);

when we call this upon the player turning the crank, we pass in a percent value from 0-100 which is indicative of how much ammunition the player now has and we use remap (which is just an inverse lerp fed into a lerp) to literally remap that value between a range of 300 and 1000, which will make the pitch of the reload sound go up as the player cranks more.

i love this kind of application of maths so much in games, and honestly i feel like not enough people actually are aware of it. if this is the first time you've heard of this, please do give it a go! it's so much fun, and there's far more uses for this kind of maths than you may have even realised.


Love these examples!

I'm doing this sort of thing in Circular for sound pitch, circle size (animation), speed/difficultly ramping and more.

My (Lua) code isn't as succinct as yours, I'll try to get better!

1 Like