Like a spring?
I know that the easing algorithm ‘elastic’ looks a bit like a spring:
The algorithm for which is:
#region Elastic Ease
public static float ElasticEaseIn(float t, float b, float c, float d)
{
return ElasticEaseInFull(t, b, c, d, 0, 0);
}
public static float ElasticEaseOut(float t, float b, float c, float d)
{
return ElasticEaseOutFull(t, b, c, d, 0, 0);
}
public static float ElasticEaseInOut(float t, float b, float c, float d)
{
return ElasticEaseInOutFull(t, b, c, d, 0, 0);
}
public static float ElasticEaseInFull(float t, float b, float c, float d, float a, float p)
{
float s;
if (t == 0) return b; if ((t /= d) == 1) return b + c;
if (p != 0) p = d * 0.3f;
if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
else s = p / _2PI * (float)Math.Asin(c / a);
return -(a * (float)Math.Pow(2, 10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p)) + b;
}
public static float ElasticEaseOutFull(float t, float b, float c, float d, float a = 0, float p = 0)
{
float s;
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (p != 0) p = d * 0.3f;
if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
else s = p / _2PI * (float)Math.Asin(c / a);
return (a * (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t * d - s) * _2PI / p) + c + b);
}
public static float ElasticEaseInOutFull(float t, float b, float c, float d, float a = 0, float p = 0)
{
float s;
if (t == 0) return b; if ((t /= d / 2) == 2) return b + c;
if (p != 0) p = d * (0.3f * 1.5f);
if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
else s = p / _2PI * (float)Math.Asin(c / a);
if (t < 1) return -.5f * (a * (float)Math.Pow(2, 10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p)) + b;
return a * (float)Math.Pow(2, -10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p) * .5f + c + b;
}
#endregion
Which you can find here with several of the other algorithms (as well as my tween engine):
using UnityEngine;
using System;
namespace com.spacepuppy.Tween
{
/// <summary>
/// Represents an eased interpolation w/ respect to time.
///
/// float t, float b, float c, float d
/// </summary>
/// <param name="current">how long into the ease are we</param>
/// <param name="initialValue">starting value if current were 0</param>
/// <param name="totalChange">total change in the value (not the end value, but the end - start)</param>
/// <param name="duration">the total amount of time (when current == duration, the returned value will == initial + totalChange)</param>
/// <returns></returns>
public delegate float Ease(float current, float initialValue, float totalChange, float duration);
public enum EaseStyle : int
{
This file has been truncated. show original
Ease methods are called as a function of time and length, where the 4 parameters are:
currentTime (t) - the currentTime into the ease we are, each from tally up the change in time and pass it in here
initialValue (b) - the starting point, say you wanted to move from 5 to 15, 5 is the start value.
totalChange (c) - the amount of change that should occur from initialValue, so from 5 to 15, 10 is the totalChange
duration (d) - the total amount of time that the ease takes place, when currentTime = duration it should return initialValue + totalChange.