Easing along, one step at a time

Published March 02, 2013
Advertisement
I just finished adding some of the standard Ease equations to my code base for use in Of Stranger Flames.
In discussion about the equations, it was requested for me to share the code here. This is not a tutorial, I don't have enough familiarity with the subject (or with math in general) to explain how every equation works. I'm just posting what I added to my own code, so others can use it if they want to.

The equations are mostly from Robert Penner's eases, which are under the BSD license. His code seems to be the ones that are used by most software, such as the jQuery, Flash, and other libraries.

Most Ease equations have the function signature:void equation(currentTime, start, distance, totalTime);
I personally like having my eases in start at 0.0, end at 1.0, and have a 'duration' of 1.0, and then I scale the input and output as desired, instead of having the equation scale it. It just makes it easier for me to understand.

That said, all my functions have the format:void equation(position);
...with 'position' being from 0.0 to 1.0.

Because Eases come in various forms (You can ease IN, OUT, IN and OUT, or OUT and IN), I made sure all the ease equations were in EaseIn form, and I created a few functions to convert the input and output to the other forms as desired.

Here's the conversion code:
http://ideone.com/6rc0Zk ([size=2]Posted as links, because all my code kept getting unformatted when hitting 'publish')

EaseFunction is: "typedef std::function EaseFunction;", which nicely permits the use of things like lambdas and functors as well as the regular ease functions. If you don't have C++11, you can just typedef it as: "typedef float(*EaseFunction)(float);" or whatever the correct C-style function-pointer syntax is.

Some of the ease equations have a few extra parameters, like ElasticEase and BackEase, so I provided some C++11 templates to convert the functions to std::functions that meet the function signature shared by the other functions. It's just a wrapper around std::bind for convenience. (I'm big on convenience! wink.png)
http://ideone.com/LPEvC6

You don't have to use C++11 to use the equations! Just the helper functions above.

I also use this function for scaling the output:
http://ideone.com/fhYHJT

Actually, mine looks like this:
http://ideone.com/hucHWu
...but that depends on a 'FloatRange' helper class I made, whereas the previous one has no dependencies.

I actually made two ScaledEase overloads, one for my Point class, and one for my Color class. I haven't tested those out yet. smile.png

I also made a Easer class, that handles most of everything inside of it. This is more for when you want persistent ease data wrapped nicely, and this class would be a member-variable of another class.

Easer.h
Easer.cpp

That also uses FloatRange, but can be easily adjusted to just use 'float begin' and 'float end' instead.

The Ease Equations


And here are a few additional ones I keep inline

The next page has graphs generated to test/demonstrate the output.

[page]

Cubic Ease In

cubiceaseeasein.png

Cubic Ease InOut

cubiceaseeaseinout.png

Cubic Ease Out

cubiceaseeaseout.png

Cubic Ease OutIn

cubiceaseeaseoutin.png

Elastic Ease In

elasticeaseeasein.png

Elastic Ease InOut

elasticeaseeaseinout.png

Elastic Ease Out

elasticeaseeaseout.png

Elastic Ease OutIn

elasticeaseeaseoutin.png

Exponential Ease In

exponentialeaseeasein.png

Exponential Ease InOut

exponentialeaseeaseinou.png

Exponential Ease Out

exponentialeaseeaseout.png

Exponential Ease OutIn

exponentialeaseeaseouti.png

Linear Ease In / InOut / Out / OutIn

lineareaseeasein.png

Power Ease In
powereaseeasein.png

Power Ease InOut

powereaseeaseinout.png

Power Ease Out

powereaseeaseout.png

Power Ease OutIn

powereaseeaseoutin.png

Quadratic Ease In

quadraticeaseeasein.png

Quadratic Ease InOut

quadraticeaseeaseinout.png

Quadratic Ease Out

quadraticeaseeaseout.png

Quadratic Ease OutIn

quadraticeaseeaseoutin.png

Quartic Ease In

quarticeaseeasein.png

Quartic Ease InOut

quarticeaseeaseinout.png

Quartic Ease Out

quarticeaseeaseout.png

Quartic Ease OutIn

quarticeaseeaseoutin.png

Quintic Ease In

quinticeaseeasein.png

Quintic Ease InOut

quinticeaseeaseinout.png

Quintic Ease Out

quinticeaseeaseout.png

Quintic Ease OutIn

quinticeaseeaseoutin.png

Sine Ease In

sineeaseeasein.png

Sine Ease InOut

sineeaseeaseinout.png

Sine Ease Out

sineeaseeaseout.png

Sine Ease OutIn

sineeaseeaseoutin.png

SmoothStep In

smoothstepeasein.png

SmoothStep InOut

smoothstepeaseinout.png

SmoothStep Out

smoothstepeaseout.png

SmoothStep OutIn

smoothstepeaseoutin.png

WeightedAverage In

weightedaverageeasein.png

WeightedAverage InOut

weightedaverageeaseinou.png

WeightedAverage Out

weightedaverageeaseout.png

WeightedAverage OutIn

weightedaverageeaseouti.png

BackEase In

backeaseeasein.png

BackEase InOut

backeaseeaseinout.png

BackEase Out

backeaseeaseout.png

BackEase OutIn

backeaseeaseoutin.png

BounceEase In

bounceeaseeaseing.png

BounceEase InOut

bounceeaseeaseinout.png

BounceEase Out

bounceeaseeaseout.png

BounceEase OutIn

bounceeaseeaseoutin.png

CircleEase In

circleeaseeasein.png

CircleEase InOut

circleeaseeaseinout.png

CircleEase Out

circleeaseeaseout.png

CircleEase OutIn

circleeaseeaseoutin.png

[page]

BounceEase was annoying me alot. I was trying to figure out how it worked, but I'm not very good with math and algorithm-thinking.

The original code looked like this:
http://ideone.com/14kyLm

And trying to figure out all the magic numbers, I eventually broke it down to this:
http://ideone.com/wGCTU9

I never figured out how the '7.5625f' value was calculated, but the others are resolved. laugh.png

Unfortunately, changing the value of 'bounciness' or 'bounces' produces poor results (probably from the constant!), so I didn't bother including the code with the rest of the equations.
custombounceeaseeasein.png


Links that were useful in understanding and converting the equations:
http://www.robertpenner.com/easing/ (The guy who made most of these now-common equations)

http://sol.gfxile.net/interpolation/index.html (also has good descriptions and explanations)
http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html
http://msdn.microsoft.com/en-us/library/ee308751.aspx

Some nice graphs:
http://easings.net/
2 likes 2 comments

Comments

SiddharthBhat

I prefer to make the the whole thing template based - so I can interpolate floats, ints, doubles, vectors as well as quaternions (anything that has the + and * operator overloaded :D And yeah, interpolators are awesome :) here's my code : http://ideone.com/RTEmf5

March 04, 2013 06:48 AM
Servant of the Lord

Good idea about the templates! I think I'll leave that for some future, though.

March 04, 2013 06:53 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement