Log In  

Cart #easingcheatsheet-2 | 2020-11-28 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
37

load #easingcheatsheet

in the Pico-8 command line to load it straight into your Pico-8 application!

Easing functions are ways of modifying the rate of change for a linear interpolation function. You may have used these in animation programs (unity, blender, etc.) by manually editing animation curves. This editing isn't all that possible without a large engine backend, but you can easily create short math equations to imitate some of the commonly used curves. This is what I've done here, implementing a set of functions for these curves, and creating a little demo so you can see how each of them changes your interpolation!

Code

All of these functions are completely self sufficient (don't rely on anything outside the function), so you can easily pick and choose which ones you want! (476 tokens for all of them)
(also, none of the functions have any safety regulations, so passing values outside of 0-1 will result in unintended behavior. Be careful! you could also add t=mid(0,t,1) to the start of the functions to clamp the values)

Quadratic functions

function easeinquad(t)
    return t*t
end

function easeoutquad(t)
    t-=1
    return 1-t*t
end

function easeinoutquad(t)
    if(t<.5) then
        return t*t*2
    else
        t-=1
        return 1-t*t*2
    end
end

function easeoutinquad(t)
    if t<.5 then
        t-=.5
        return .5-t*t*2
    else
        t-=.5
        return .5+t*t*2
    end
end

Quartic functions

(these are very similar to quadratics, but flatter at the start and steeper towards the end)

function easeinquart(t)
    return t*t*t*t
end

function easeoutquart(t)
    t-=1
    return 1-t*t*t*t
end

function easeinoutquart(t)
    if t<.5 then
        return 8*t*t*t*t
    else
        t-=1
        return (1-8*t*t*t*t)
    end
end

function easeoutinquart(t)
    if t<.5 then
        t-=.5
        return .5-8*t*t*t*t
    else
        t-=.5
        return .5+8*t*t*t*t
    end
end

Overshooting functions

(these functions overshoot the range slightly and then return to it)

function easeinovershoot(t)
    return 2.7*t*t*t-1.7*t*t
end

function easeoutovershoot(t)
    t-=1
    return 1+2.7*t*t*t+1.7*t*t
end

function easeinoutovershoot(t)
    if(t<.5) then
        return (2.7*8*t*t*t-1.7*4*t*t)/2
    else
        t-=1
        return 1+(2.7*8*t*t*t+1.7*4*t*t)/2
    end
end

function easeoutinovershoot(t)
    if t<.5 then
        t-=.5
        return (2.7*8*t*t*t+1.7*4*t*t)/2+.5
    else
        t-=.5
        return (2.7*8*t*t*t-1.7*4*t*t)/2+.5
    end
end

Elastic functions

(these functions overshoot slightly and then oscillate near the edges of the range, like an elastic band)

function easeinelastic(t)
    if(t==0) return 0
    return 2^(10*t-10)*cos(2*t-2)
end

function easeoutelastic(t)
    if(t==1) return 1
    return 1-2^(-10*t)*cos(2*t)
end

function easeinoutelastic(t)
    if t<.5 then
        return 2^(10*2*t-10)*cos(2*2*t-2)/2
    else
        t-=.5
        return 1-2^(-10*2*t)*cos(2*2*t)/2
    end
end

function easeoutinelastic(t)
    if t<.5 then
        return .5-2^(-10*2*t)*cos(2*2*t)/2
    else
        t-=.5
        return 2^(10*2*t-10)*cos(2*2*t-2)/2+.5
    end
end

Bouncing functions

(these functions hit the edge values early, then bounce back a few times)

function easeinbounce(t)
    t=1-t
    local n1=7.5625
    local d1=2.75

    if (t<1/d1) then
        return 1-n1*t*t;
    elseif(t<2/d1) then
        t-=1.5/d1
        return 1-n1*t*t-.75;
    elseif(t<2.5/d1) then
        t-=2.25/d1
        return 1-n1*t*t-.9375;
    else
        t-=2.625/d1
        return 1-n1*t*t-.984375;
    end
end

function easeoutbounce(t)
    local n1=7.5625
    local d1=2.75

    if (t<1/d1) then
        return n1*t*t;
    elseif(t<2/d1) then
        t-=1.5/d1
        return n1*t*t+.75;
    elseif(t<2.5/d1) then
        t-=2.25/d1
        return n1*t*t+.9375;
    else
        t-=2.625/d1
        return n1*t*t+.984375;
    end
end

--(ease in+out function omitted, it doesn't really make sense for any uses IMO)

Other useful functions:

--(linear interpolation between a/b)
function lerp(a,b,t)
    return a+(b-a)*t
end

--(finds the t value that would
--return v in a lerp between a/b)
function invlerp(a,b,v)
    return (v-a)/(b-a)
end

Crediting:

If you use these in your own code, please just put a link to this BBS post in a comment near the function! I want to allow anyone reading your code to be able to find this and use it as well, for learning purposes.
All of the easing functions in this cart are based/translated from this site: https://easings.net/

Also, feel free to suggest any functions I'm missing! I omitted some of the functions that I thought were redundant/not commonly used, but correct me if I was wrong with that assumption!

P#84765 2020-11-27 21:13 ( Edited 2020-12-02 14:20)

1

This is such a nice resource, thanks for sharing! For those of us who haven't done "real" math in more than 30 years, this is a huge help.

P#84778 2020-11-27 22:53

This is beautiful! Thank you!

P#84787 2020-11-28 08:14

This is a super resource.
Thanks for creating it! 🤓👍

P#84954 2020-12-02 19:45

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2021-04-18 00:19 | 0.023s | 4194k | Q:32