Games-by-Mason/Tween
Common easing and interpolation functions for game development.
Rigid motion often appears unnatural. This library provides easing and interpolation functions widely used in game development to create natural looking or stylized tweens ("inbetweens").
Example usage:
const tween = @import("tween");
const lerp = tween.interp.lerp;
const ease = tween.ease;
// ...
pos = lerp(start, end, ease.bounceOut(t));
The following interpolation functions are provided:
lerp
ilerp
remap
clamp01
damp
Linear interpolation differs from the implementation in the standard library in that it returns exact results at 0 and 1.
Additionally, it accepts a larger variety of types:
ilerp
is the inverse of lerp
, and only accepts floats. remap
uses a combination of ilerp
and lerp
to remap a value from one range to another.
clamp01
is what it sounds like, and is provided for convenience. You can apply it to a t
value getting passed to lerp
to create a clamped lerp.
damp
is useful for framerate independent lerping.
If you're unfamiliar with this class of functions, I recommend viewing The Simple Yet Powerful Math We Don't Talk About by Freya Holmer.
The following easing functions are supported, most of these originated with Robert Penner:
I've opted to not include GIFs demonstrating the easing styles here, as easings.net already has a great visualizer for almost all of these.
(Note that the links above are provided for convenient reference, the actual implementations may not be 100% identical or may have slightly different parameters.)
In, out, and in-out variations of each are provided, functions are exact at 0 and 1 for 32 bit floats unless otherwise noted.
Easing functions operate on the t
value given to lerp
. For example:
const result = lerp(a, b, ease.smootherstep(t));
If you're new to easing and not sure which to use, smootherstep
is a reasonable default to slap on everything to start.
You can adapt easing functions with mix
, combine
, reflect
, and reverse
.
The provided easing functions are exact at 0 and 1 for 32 bit floats unless otherwise noted.
When designing your own easing functions, I highly recommend testing them in Desmos.
If you're shipping binaries, you probably have a min spec CPU in mind. I recommend making sure muladd
is enabled for your baseline so it doesn't end up getting emulated in software, more info here. On x86 this means enabling fma
, on arm/aarch64 this means enabling either neon
or vfp4
.