### Author Topic: Easing Functions  (Read 4222 times)

#### aonyn

• Mr. Polyvector
• Posts: 136
##### Easing Functions
« on: 2011-Mar-12 »
Hi All,

Here is a collection of useful easing functions. I wanted these for a personal project, and decided to post them here as well, in hopes that they can be useful to others as well.
I translated these to GLBasic from the open source actionscript easing functions by Robert Penner. (www.robertpenner.com).

Also included are two mixing functions I added, which can be useful with this library, particularly the inverse mixing function.

Finally I included a sample of usage which includes the inverse mixing function, so you can see how they can be used together.
And of course a screenshot of the output of the sample.

regards,
Dave

Code: GLBasic [Select]
// ---------------------------------------------------------
// Mixing and Inverse Mixing Functions
// ---------------------------------------------------------
// by David Marconi
// ---------------------------------------------------------

// a = start value
// b = end value
// v = percent between values to return

// return value at percentage(v) between (a) and (b)
FUNCTION mix: a#, b#, v#
RETURN ((b*v) / 100.0) + (a * (100.0-v) / 100.0)
ENDFUNCTION

// return value at inverse percentage(v) between (a) and (b)
FUNCTION mixInverse: a#, b#, v#
RETURN ((a*v) / 100.0) + (b * (100.0 -v) / 100.0)
ENDFUNCTION

// ---------------------------------------------------------
// Easing Functions
// ---------------------------------------------------------
// Based on Actionscript by Robert Penner
// www.robertpenner.com
// ---------------------------------------------------------
//
//Open source under the BSD License.
//
//
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---------------------------------------------------------
// Translated to GLBasic by David Marconi
// ---------------------------------------------------------

// t = current time
// b = start value
// c = change in value
// d = duration

// simple linear tweening - no easing, no acceleration
FUNCTION linearTween: t#, b#, c#, d#
RETURN c*t/d + b
ENDFUNCTION

// quadratic easing in - accelerating from zero velocity
FUNCTION easeInQuad: t#, b#, c#, d#
t = t/d
RETURN c*t*t + b
ENDFUNCTION

// quadratic easing out - decelerating to zero velocity
FUNCTION easeOutQuad: t#, b#, c#, d#
t = t / d
RETURN -c * t*(t-2) + b
ENDFUNCTION

// quadratic easing in/out - acceleration until halfway, then deceleration
FUNCTION easeInOutQuad: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN c/2*t*t + b
ENDIF
DEC t
RETURN -c/2 * (t*(t-2) - 1) + b
ENDFUNCTION

// cubic easing in - accelerating from zero velocity
FUNCTION easeInCubic: t#, b#, c#, d#
t = t/d
RETURN c*t*t*t + b
ENDFUNCTION

// cubic easing out - decelerating to zero velocity
FUNCTION easeOutCubic: t#, b#, c#, d#
t = t/d
DEC t
RETURN c*(t*t*t + 1) + b
ENDFUNCTION

// cubic easing in/out - acceleration until halfway, then deceleration
FUNCTION easeInOutCubic: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN c/2*t*t*t + b
ENDIF
DEC t, 2
RETURN c/2*(t*t*t + 2) + b
ENDFUNCTION

// quartic easing in - accelerating from zero velocity
FUNCTION easeInQuart: t#, b#, c#, d#
t = t/d
RETURN c*t*t*t*t + b
ENDFUNCTION

// quartic easing out - decelerating to zero velocity
FUNCTION easeOutQuart: t#, b#, c#, d#
t = t/d
DEC t
RETURN -c * (t*t*t*t - 1) + b
ENDFUNCTION

// quartic easing in/out - acceleration until halfway, then deceleration
FUNCTION easeInOutQuart: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN c/2*t*t*t*t + b
ENDIF
DEC t, 2
RETURN -c/2 * (t*t*t*t - 2) + b
ENDFUNCTION

// quintic easing in - accelerating from zero velocity
FUNCTION easeInQuint: t#, b#, c#, d#
t = t/d
RETURN c*t*t*t*t*t + b
ENDFUNCTION

// quintic easing out - decelerating to zero velocity
FUNCTION easeOutQuint: t#, b#, c#, d#
t = t/d
DEC t
RETURN c*(t*t*t*t*t + 1) + b
ENDFUNCTION

// quintic easing in/out - acceleration until halfway, then deceleration
FUNCTION easeInOutQuint: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN c/2*t*t*t*t*t + b
ENDIF
DEC t, 2
RETURN c/2*(t*t*t*t*t + 2) + b
ENDFUNCTION

// sinusoidal easing in - accelerating from zero velocity
FUNCTION easeInSine: t#, b#, c#, d#
LOCAL pi# = 3.1415926535
RETURN -c * COS(t/d * (pi/2)) + c + b
ENDFUNCTION

// sinusoidal easing out - decelerating to zero velocity
FUNCTION easeOutSine: t#, b#, c#, d#
LOCAL pi# = 3.1415926535
RETURN c * SIN(t/d * (pi/2)) + b
ENDFUNCTION

// sinusoidal easing in/out - accelerating until halfway, then decelerating
FUNCTION easeInOutSine: t#, b#, c#, d#
LOCAL pi# = 3.1415926535
RETURN -c/2 * (COS(pi*t/d) - 1) + b
ENDFUNCTION

// exponential easing in - accelerating from zero velocity
FUNCTION easeInExpo: t#, b#, c#, d#
RETURN c * POW(2, 10 * (t/d - 1)) + b
ENDFUNCTION

// exponential easing out - decelerating to zero velocity
FUNCTION easeOutExpo: t#, b#, c#, d#
RETURN c * (-POW(2, -10 * t/d) + 1) + b
ENDFUNCTION

// exponential easing in/out - accelerating until halfway, then decelerating
FUNCTION easeInOutExpo: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN c/2 * POW(2, 10 * (t - 1)) + b
ENDIF
DEC t
RETURN c/2 * (-POW(2, -10 * t) + 2) + b
ENDFUNCTION

// circular easing in - accelerating from zero velocity
FUNCTION easeInCirc: t#, b#, c#, d#
t = t/d
RETURN -c * (SQR(1 - t*t) - 1) + b
ENDFUNCTION

// circular easing out - decelerating to zero velocity
FUNCTION easeOutCirc: t#, b#, c#, d#
t = t/d
DEC t
RETURN c * SQR(1 - t*t) + b
ENDFUNCTION

// circular easing in/out - acceleration until halfway, then deceleration
FUNCTION easeInOutCirc: t#, b#, c#, d#
t = t / (d/2)
IF t < 1
RETURN -c/2 * (SQR(1 - t*t) - 1) + b
ENDIF
DEC t, 2
RETURN c/2 * (SQR(1 - t*t) + 1) + b
ENDFUNCTION

Code: GLBasic [Select]
// Sample using easing functions to create a curve
// Also uses mixInverse function to assist easing of a > to < value range
FOR x=0 TO 99
DRAWRECT linearTween(x, 0, 640, 99), mixInverse(0, 480, easeInQuad(x, 0, 99, 99)), 2, 2, RGB(255, 255, 255)
NEXT
SHOWSCREEN
MOUSEWAIT
END

For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

#### Moru

• Prof. Inline
• Posts: 1774
##### Re: Easing Functions
« Reply #1 on: 2011-Mar-12 »
Very nice, added to the library of useful stuff :-)

• Prof. Inline
• Posts: 1667
##### Re: Easing Functions
« Reply #2 on: 2011-Mar-13 »
AMD X2 4600, 2 GB Ram, ATI X1950 XTX, XP PRO SP2: GLB Premium 10.beta_dingsi, <(´.´<) Kirby Dance (>`.`)>
http://lostrevenant.blogspot.com
alea iacta est

#### aonyn

• Mr. Polyvector
• Posts: 136
##### Re: Easing Functions
« Reply #3 on: 2011-Mar-13 »
Hi all,

You are quite welcome.
I am glad that this will be useful to other users.

regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

#### bigsofty

• Community Developer
• Prof. Inline
• Posts: 2626
##### Re: Easing Functions
« Reply #4 on: 2011-Mar-13 »
Handy, thanks Dave!

Cheers,

Ian
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)