easings.d 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*******************************************************************************************
  2. *
  3. * raylib easings (header only file)
  4. *
  5. * Useful easing functions for values animation
  6. *
  7. * This header uses:
  8. * #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster.
  9. * // This requires lots of memory on system.
  10. * How to use:
  11. * The four inputs t,b,c,d are defined as follows:
  12. * t = current time (in any unit measure, but same unit as duration)
  13. * b = starting value to interpolate
  14. * c = the total change in value of b that needs to occur
  15. * d = total time it should take to complete (duration)
  16. *
  17. * Example:
  18. *
  19. * int currentTime = 0;
  20. * int duration = 100;
  21. * float startPositionX = 0.0f;
  22. * float finalPositionX = 30.0f;
  23. * float currentPositionX = startPositionX;
  24. *
  25. * while (currentPositionX < finalPositionX)
  26. * {
  27. * currentPositionX = EaseSineIn(currentTime, startPositionX, finalPositionX - startPositionX, duration);
  28. * currentTime++;
  29. * }
  30. *
  31. * A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/)
  32. *
  33. * Robert Penner License
  34. * ---------------------------------------------------------------------------------
  35. * Open source under the BSD License.
  36. *
  37. * Copyright (c) 2001 Robert Penner. All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without modification,
  40. * are permitted provided that the following conditions are met:
  41. *
  42. * - Redistributions of source code must retain the above copyright notice,
  43. * this list of conditions and the following disclaimer.
  44. * - Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. * - Neither the name of the author nor the names of contributors may be used
  48. * to endorse or promote products derived from this software without specific
  49. * prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  52. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  53. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  54. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  55. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  56. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  59. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  60. * OF THE POSSIBILITY OF SUCH DAMAGE.
  61. * ---------------------------------------------------------------------------------
  62. *
  63. * Copyright (c) 2015 Ramon Santamaria
  64. *
  65. * This software is provided "as-is", without any express or implied warranty. In no event
  66. * will the authors be held liable for any damages arising from the use of this software.
  67. *
  68. * Permission is granted to anyone to use this software for any purpose, including commercial
  69. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  70. *
  71. * 1. The origin of this software must not be misrepresented; you must not claim that you
  72. * wrote the original software. If you use this software in a product, an acknowledgment
  73. * in the product documentation would be appreciated but is not required.
  74. *
  75. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  76. * as being the original software.
  77. *
  78. * 3. This notice may not be removed or altered from any source distribution.
  79. *
  80. **********************************************************************************************/
  81. extern (C): // NOTE: By default, compile functions as static inline
  82. import core.stdc.math; // Required for: sinf(), cosf(), sqrt(), pow()
  83. enum PI = 3.14159265358979323846f; //Required as PI is not always defined in math.h
  84. // Linear Easing functions
  85. static float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
  86. static float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
  87. static float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
  88. static float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
  89. // Sine Easing functions
  90. static float EaseSineIn(float t, float b, float c, float d) { return (-c*cosf(t/d*(PI/2.0f)) + c + b); }
  91. static float EaseSineOut(float t, float b, float c, float d) { return (c*sinf(t/d*(PI/2.0f)) + b); }
  92. static float EaseSineInOut(float t, float b, float c, float d) { return (-c/2.0f*(cosf(PI*t/d) - 1.0f) + b); }
  93. // Circular Easing functions
  94. static float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrtf(1.0f - t*t) - 1.0f) + b); }
  95. static float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrtf(1.0f - t*t) + b); }
  96. static float EaseCircInOut(float t, float b, float c, float d)
  97. {
  98. if ((t/=d/2.0f) < 1.0f) return (-c/2.0f*(sqrtf(1.0f - t*t) - 1.0f) + b);
  99. t -= 2.0f; return (c/2.0f*(sqrtf(1.0f - t*t) + 1.0f) + b);
  100. }
  101. // Cubic Easing functions
  102. static float EaseCubicIn(float t, float b, float c, float d) { t /= d; return (c*t*t*t + b); }
  103. static float EaseCubicOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*(t*t*t + 1.0f) + b); }
  104. static float EaseCubicInOut(float t, float b, float c, float d)
  105. {
  106. if ((t/=d/2.0f) < 1.0f) return (c/2.0f*t*t*t + b);
  107. t -= 2.0f; return (c/2.0f*(t*t*t + 2.0f) + b);
  108. }
  109. // Quadratic Easing functions
  110. static float EaseQuadIn(float t, float b, float c, float d) { t /= d; return (c*t*t + b); }
  111. static float EaseQuadOut(float t, float b, float c, float d) { t /= d; return (-c*t*(t - 2.0f) + b); }
  112. static float EaseQuadInOut(float t, float b, float c, float d)
  113. {
  114. if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b);
  115. return (-c/2.0f*(((t - 1.0f)*(t - 3.0f)) - 1.0f) + b);
  116. }
  117. // Exponential Easing functions
  118. static float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*powf(2.0f, 10.0f*(t/d - 1.0f)) + b); }
  119. static float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-powf(2.0f, -10.0f*t/d) + 1.0f) + b); }
  120. static float EaseExpoInOut(float t, float b, float c, float d)
  121. {
  122. if (t == 0.0f) return b;
  123. if (t == d) return (b + c);
  124. if ((t/=d/2.0f) < 1.0f) return (c/2.0f*powf(2.0f, 10.0f*(t - 1.0f)) + b);
  125. return (c/2.0f*(-powf(2.0f, -10.0f*(t - 1.0f)) + 2.0f) + b);
  126. }
  127. // Back Easing functions
  128. static float EaseBackIn(float t, float b, float c, float d)
  129. {
  130. float s = 1.70158f;
  131. float postFix = t/=d;
  132. return (c*(postFix)*t*((s + 1.0f)*t - s) + b);
  133. }
  134. static float EaseBackOut(float t, float b, float c, float d)
  135. {
  136. float s = 1.70158f;
  137. t = t/d - 1.0f;
  138. return (c*(t*t*((s + 1.0f)*t + s) + 1.0f) + b);
  139. }
  140. static float EaseBackInOut(float t, float b, float c, float d)
  141. {
  142. float s = 1.70158f;
  143. if ((t/=d/2.0f) < 1.0f)
  144. {
  145. s *= 1.525f;
  146. return (c/2.0f*(t*t*((s + 1.0f)*t - s)) + b);
  147. }
  148. float postFix = t-=2.0f;
  149. s *= 1.525f;
  150. return (c/2.0f*((postFix)*t*((s + 1.0f)*t + s) + 2.0f) + b);
  151. }
  152. // Bounce Easing functions
  153. static float EaseBounceOut(float t, float b, float c, float d)
  154. {
  155. if ((t/=d) < (1.0f/2.75f))
  156. {
  157. return (c*(7.5625f*t*t) + b);
  158. }
  159. else if (t < (2.0f/2.75f))
  160. {
  161. float postFix = t-=(1.5f/2.75f);
  162. return (c*(7.5625f*(postFix)*t + 0.75f) + b);
  163. }
  164. else if (t < (2.5/2.75))
  165. {
  166. float postFix = t-=(2.25f/2.75f);
  167. return (c*(7.5625f*(postFix)*t + 0.9375f) + b);
  168. }
  169. else
  170. {
  171. float postFix = t-=(2.625f/2.75f);
  172. return (c*(7.5625f*(postFix)*t + 0.984375f) + b);
  173. }
  174. }
  175. static float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d - t, 0.0f, c, d) + b); }
  176. static float EaseBounceInOut(float t, float b, float c, float d)
  177. {
  178. if (t < d/2.0f) return (EaseBounceIn(t*2.0f, 0.0f, c, d)*0.5f + b);
  179. else return (EaseBounceOut(t*2.0f - d, 0.0f, c, d)*0.5f + c*0.5f + b);
  180. }
  181. // Elastic Easing functions
  182. static float EaseElasticIn(float t, float b, float c, float d)
  183. {
  184. if (t == 0.0f) return b;
  185. if ((t/=d) == 1.0f) return (b + c);
  186. float p = d*0.3f;
  187. float a = c;
  188. float s = p/4.0f;
  189. float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
  190. return (-(postFix*sinf((t*d-s)*(2.0f*PI)/p )) + b);
  191. }
  192. static float EaseElasticOut(float t, float b, float c, float d)
  193. {
  194. if (t == 0.0f) return b;
  195. if ((t/=d) == 1.0f) return (b + c);
  196. float p = d*0.3f;
  197. float a = c;
  198. float s = p/4.0f;
  199. return (a*powf(2.0f,-10.0f*t)*sinf((t*d-s)*(2.0f*PI)/p) + c + b);
  200. }
  201. static float EaseElasticInOut(float t, float b, float c, float d)
  202. {
  203. if (t == 0.0f) return b;
  204. if ((t/=d/2.0f) == 2.0f) return (b + c);
  205. float p = d*(0.3f*1.5f);
  206. float a = c;
  207. float s = p/4.0f;
  208. if (t < 1.0f)
  209. {
  210. float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
  211. return -0.5f*(postFix*sinf((t*d-s)*(2.0f*PI)/p)) + b;
  212. }
  213. float postFix = a*powf(2.0f, -10.0f*(t-=1.0f));
  214. return (postFix*sinf((t*d-s)*(2.0f*PI)/p)*0.5f + c + b);
  215. }