easing.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //! Input value between 0 and 1, returns value adjusted by easing, no automatic clamping of input(do your own !!)
  2. class Easing
  3. {
  4. static float EaseInSine( float t )
  5. {
  6. return -1 * Math.Cos( t * ( Math.PI / 2 ) ) + 1;
  7. }
  8. static float EaseOutSine( float t )
  9. {
  10. return Math.Sin( t * ( Math.PI / 2 ) );
  11. }
  12. static float EaseInOutSine( float t )
  13. {
  14. return -0.5 * ( Math.Cos( Math.PI * t ) - 1 );
  15. }
  16. static float EaseInQuad( float t )
  17. {
  18. return t * t;
  19. }
  20. static float EaseOutQuad( float t )
  21. {
  22. return t * ( 2 - t );
  23. }
  24. static float EaseInOutQuad( float t )
  25. {
  26. if(t < 0.5)
  27. return 2 * t * t;
  28. else
  29. return -1 + ( 4 - 2 * t ) * t;
  30. }
  31. static float EaseInCubic( float t )
  32. {
  33. return t * t * t;
  34. }
  35. static float EaseOutCubic( float t )
  36. {
  37. float t1 = t - 1;
  38. return t1 * t1 * t1 + 1;
  39. }
  40. static float EaseInOutCubic( float t )
  41. {
  42. if( t < 0.5 )
  43. return 4 * t * t * t;
  44. else
  45. return ( t - 1 ) * ( 2 * t - 2 ) * ( 2 * t - 2 ) + 1;
  46. }
  47. static float EaseInQuart( float t )
  48. {
  49. return t * t * t * t;
  50. }
  51. static float EaseOutQuart( float t )
  52. {
  53. float t1 = t - 1;
  54. return 1 - t1 * t1 * t1 * t1;
  55. }
  56. static float EaseInOutQuart( float t )
  57. {
  58. float t1 = t - 1;
  59. if(t < 0.5)
  60. return 8 * t * t * t * t;
  61. else
  62. return 1 - 8 * t1 * t1 * t1 * t1;
  63. }
  64. static float EaseInQuint( float t )
  65. {
  66. return t * t * t * t * t;
  67. }
  68. static float EaseOutQuint( float t )
  69. {
  70. float t1 = t - 1;
  71. return 1 + t1 * t1 * t1 * t1 * t1;
  72. }
  73. static float EaseInOutQuint( float t )
  74. {
  75. float t1 = t - 1;
  76. if( t < 0.5 )
  77. {
  78. return 16 * t * t * t * t * t;
  79. }
  80. else
  81. {
  82. return 1 + 16 * t1 * t1 * t1 * t1 * t1;
  83. }
  84. }
  85. static float EaseInExpo( float t )
  86. {
  87. if( t == 0 )
  88. {
  89. return 0;
  90. }
  91. return Math.Pow( 2, 10 * ( t - 1 ) );
  92. }
  93. static float EaseOutExpo( float t )
  94. {
  95. if( t == 1 ) {
  96. return 1;
  97. }
  98. return ( -Math.Pow( 2, -10 * t ) + 1 );
  99. }
  100. static float EaseInOutExpo( float t )
  101. {
  102. if( t == 0 || t == 1 )
  103. {
  104. return t;
  105. }
  106. float scaledTime = t * 2;
  107. float scaledTime1 = scaledTime - 1;
  108. if( scaledTime < 1 )
  109. {
  110. return 0.5 * Math.Pow( 2, 10 * scaledTime1 );
  111. }
  112. return 0.5 * ( -Math.Pow( 2, -10 * scaledTime1 ) + 2 );
  113. }
  114. static float EaseInCirc( float t )
  115. {
  116. float scaledTime = t / 1;
  117. return -1 * ( Math.Sqrt( 1 - scaledTime * t ) - 1 );
  118. }
  119. static float EaseOutCirc( float t )
  120. {
  121. float t1 = t - 1;
  122. return Math.Sqrt( 1 - t1 * t1 );
  123. }
  124. static float EaseInOutCirc( float t )
  125. {
  126. float scaledTime = t * 2;
  127. float scaledTime1 = scaledTime - 2;
  128. if( scaledTime < 1 )
  129. {
  130. return -0.5 * ( Math.Sqrt( 1 - scaledTime * scaledTime ) - 1 );
  131. }
  132. return 0.5 * ( Math.Sqrt( 1 - scaledTime1 * scaledTime1 ) + 1 );
  133. }
  134. static float EaseInBack( float t, float magnitude = 1.70158 )
  135. {
  136. return t * t * ( ( magnitude + 1 ) * t - magnitude );
  137. }
  138. static float EaseOutBack( float t, float magnitude = 1.70158 )
  139. {
  140. float scaledTime = ( t / 1 ) - 1;
  141. return (scaledTime * scaledTime * ( ( magnitude + 1 ) * scaledTime + magnitude )) + 1;
  142. }
  143. static float EaseInOutBack( float t, float magnitude = 1.70158 )
  144. {
  145. float scaledTime = t * 2;
  146. float scaledTime2 = scaledTime - 2;
  147. float s = magnitude * 1.525;
  148. if( scaledTime < 1)
  149. {
  150. return 0.5 * scaledTime * scaledTime * (( ( s + 1 ) * scaledTime ) - s);
  151. }
  152. return 0.5 * (scaledTime2 * scaledTime2 * ( ( s + 1 ) * scaledTime2 + s ) + 2);
  153. }
  154. static float EaseInElastic( float t, float magnitude = 0.7 )
  155. {
  156. if( t == 0 || t == 1 )
  157. return t;
  158. float scaledTime = t / 1;
  159. float scaledTime1 = scaledTime - 1;
  160. float p = 1 - magnitude;
  161. float s = p / ( 2 * Math.PI ) * Math.Asin( 1 );
  162. return -(Math.Pow( 2, 10 * scaledTime1 ) * Math.Sin( ( scaledTime1 - s ) * ( 2 * Math.PI ) / p ));
  163. }
  164. static float EaseOutElastic( float t, float magnitude = 0.7 )
  165. {
  166. float p = 1 - magnitude;
  167. float scaledTime = t * 2;
  168. if( t == 0 || t == 1 ) {
  169. return t;
  170. }
  171. float s = p / ( 2 * Math.PI ) * Math.Asin( 1 );
  172. return (Math.Pow( 2, -10 * scaledTime ) * Math.Sin( ( scaledTime - s ) * ( 2 * Math.PI ) / p )) + 1;
  173. }
  174. static float EaseInOutElastic( float t, float magnitude = 0.65 )
  175. {
  176. float p = 1 - magnitude;
  177. if( t == 0 || t == 1 )
  178. {
  179. return t;
  180. }
  181. float scaledTime = t * 2;
  182. float scaledTime1 = scaledTime - 1;
  183. float s = p / ( 2 * Math.PI ) * Math.Asin( 1 );
  184. if( scaledTime < 1 )
  185. {
  186. return -0.5 * (Math.Pow( 2, 10 * scaledTime1 ) * Math.Sin( ( scaledTime1 - s ) * ( 2 * Math.PI ) / p ));
  187. }
  188. return (Math.Pow( 2, -10 * scaledTime1 ) * Math.Sin( ( scaledTime1 - s ) * ( 2 * Math.PI ) / p ) * 0.5) + 1;
  189. }
  190. static float EaseOutBounce( float t )
  191. {
  192. float scaledTime = t / 1;
  193. if( scaledTime < ( 1 / 2.75 ) ) {
  194. return 7.5625 * scaledTime * scaledTime;
  195. } else if( scaledTime < ( 2 / 2.75 ) ) {
  196. float scaledTime2 = scaledTime - ( 1.5 / 2.75 );
  197. return ( 7.5625 * scaledTime2 * scaledTime2 ) + 0.75;
  198. } else if( scaledTime < ( 2.5 / 2.75 ) ) {
  199. scaledTime2 = scaledTime - ( 2.25 / 2.75 );
  200. return ( 7.5625 * scaledTime2 * scaledTime2 ) + 0.9375;
  201. } else {
  202. scaledTime2 = scaledTime - ( 2.625 / 2.75 );
  203. return ( 7.5625 * scaledTime2 * scaledTime2 ) + 0.984375;
  204. }
  205. }
  206. static float EaseInBounce( float t )
  207. {
  208. return 1 - EaseOutBounce( 1 - t );
  209. }
  210. static float EaseInOutBounce( float t )
  211. {
  212. if( t < 0.5 )
  213. {
  214. return EaseInBounce( t * 2 ) * 0.5;
  215. }
  216. return ( EaseOutBounce( ( t * 2 ) - 1 ) * 0.5 ) + 0.5;
  217. }
  218. }