Jump to content

  • Log In with Google      Sign In   
  • Create Account

Easing along, one step at a time

Posted by Servant of the Lord, in Code snippets 02 March 2013 · 1,243 views

interpolation easing tweening
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 (Posted as links, because all my code kept getting unformatted when hitting 'publish')

EaseFunction is: "typedef std::function<float(float)> 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! Posted Image)

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:

Actually, mine looks like this:
...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. Posted Image

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.


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.

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

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

Recent Entries

Recent Comments

Latest Visitors