• Advertisement
• entries
114
• comments
273
• views
598802

# Easing along, one step at a time

3044 views

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! )
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.

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

Cubic Ease InOut

Cubic Ease Out

Cubic Ease OutIn

Elastic Ease In

Elastic Ease InOut

Elastic Ease Out

Elastic Ease OutIn

Exponential Ease In

Exponential Ease InOut

Exponential Ease Out

Exponential Ease OutIn

Linear Ease In / InOut / Out / OutIn

Power Ease In

Power Ease InOut

Power Ease Out

Power Ease OutIn

Quadratic Ease In

Quadratic Ease InOut

Quadratic Ease Out

Quadratic Ease OutIn

Quartic Ease In

Quartic Ease InOut

Quartic Ease Out

Quartic Ease OutIn

Quintic Ease In

Quintic Ease InOut

Quintic Ease Out

Quintic Ease OutIn

Sine Ease In

Sine Ease InOut

Sine Ease Out

Sine Ease OutIn

SmoothStep In

SmoothStep InOut

SmoothStep Out

SmoothStep OutIn

WeightedAverage In

WeightedAverage InOut

WeightedAverage Out

WeightedAverage OutIn

BackEase In

BackEase InOut

BackEase Out

BackEase OutIn

BounceEase In

BounceEase InOut

BounceEase Out

BounceEase OutIn

CircleEase In

CircleEase InOut

CircleEase Out

CircleEase OutIn

[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.

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.

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/

## Recommended Comments

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

#### Share this comment

##### Link to comment

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

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• Advertisement