Skip to content

Instantly share code, notes, and snippets.

@wispborne
Created March 10, 2020 19:52
Show Gist options
  • Select an option

  • Save wispborne/6db23913235ebda309608c3ba8fdda98 to your computer and use it in GitHub Desktop.

Select an option

Save wispborne/6db23913235ebda309608c3ba8fdda98 to your computer and use it in GitHub Desktop.
Easing functions, converted to Kotlin
/**
* Taken from <a href="https://github.com/mattdesl/cisc226game/blob/master/SpaceGame/src/space/engine/easing/Easing.java">Github</a>
*
* @author Robert Penner (functions)
* @author davedes (java port)
* @author Wisp (kotlin port)
*/
object Easing {
object Quadratic {
/**
* Quadratic easing in - accelerating from zero velocity.
*/
fun easeIn(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return valueAtEnd * (run { t /= duration;t }) * t + valueAtStart
}
/**
* Quadratic easing out - decelerating to zero velocity.
*/
fun easeOut(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return -valueAtEnd * (run { t /= duration;t }) * (t - 2) + valueAtStart
}
/**
* Quadratic easing in/out - acceleration until halfway, then deceleration
*/
fun easeInThenOut(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return if ((run { t /= duration / 2;t }) < 1)
valueAtEnd / 2 * t * t + valueAtStart;
else -valueAtEnd / 2 * ((--t) * (t - 2) - 1) + valueAtStart;
}
}
object Linear {
/**
* Simple linear tweening - no easing.
*/
fun tween(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float =
valueAtEnd * time / duration + valueAtStart
}
object Cubic {
/**
* Cubic easing in - accelerating from zero velocity.
*/
fun easeIn(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return valueAtEnd * (run { t /= duration;t }) * t * t + valueAtStart
}
/**
* Cubic easing out - decelerating to zero velocity.
*/
fun easeOut(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return valueAtEnd * ((run { t = t / duration - 1;t }) * t * t + 1) + valueAtStart;
}
/**
* Cubic easing in/out - acceleration until halfway, then deceleration
*/
fun easeInThenOut(time: Float, valueAtStart: Float, valueAtEnd: Float, duration: Float): Float {
var t = time
return if ((run { t /= duration / 2;t }) < 1)
valueAtEnd / 2 * t * t * t + valueAtStart
else valueAtEnd / 2 * ((run { t -= 2;t }) * t * t + 2) + valueAtStart;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment