tween_interpolaters.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*************************************************************************/
  2. /* tween_interpolaters.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tween.h"
  31. const real_t pi = 3.1415926535898;
  32. ///////////////////////////////////////////////////////////////////////////
  33. // linear
  34. ///////////////////////////////////////////////////////////////////////////
  35. namespace linear {
  36. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  37. return c * t / d + b;
  38. }
  39. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  40. return c * t / d + b;
  41. }
  42. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  43. return c * t / d + b;
  44. }
  45. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  46. return c * t / d + b;
  47. }
  48. };
  49. ///////////////////////////////////////////////////////////////////////////
  50. // sine
  51. ///////////////////////////////////////////////////////////////////////////
  52. namespace sine {
  53. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  54. return -c * cos(t / d * (pi / 2)) + c + b;
  55. }
  56. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  57. return c * sin(t / d * (pi / 2)) + b;
  58. }
  59. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  60. return -c / 2 * (cos(pi * t / d) - 1) + b;
  61. }
  62. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  63. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  64. }
  65. };
  66. ///////////////////////////////////////////////////////////////////////////
  67. // quint
  68. ///////////////////////////////////////////////////////////////////////////
  69. namespace quint {
  70. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  71. return c * pow(t / d, 5) + b;
  72. }
  73. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  74. return c * (pow(t / d - 1, 5) + 1) + b;
  75. }
  76. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  77. t = t / d * 2;
  78. if (t < 1) return c / 2 * pow(t, 5) + b;
  79. return c / 2 * (pow(t - 2, 5) + 2) + b;
  80. }
  81. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  82. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  83. }
  84. };
  85. ///////////////////////////////////////////////////////////////////////////
  86. // quart
  87. ///////////////////////////////////////////////////////////////////////////
  88. namespace quart {
  89. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  90. return c * pow(t / d, 4) + b;
  91. }
  92. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  93. return -c * (pow(t / d - 1, 4) - 1) + b;
  94. }
  95. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  96. t = t / d * 2;
  97. if (t < 1) return c / 2 * pow(t, 4) + b;
  98. return -c / 2 * (pow(t - 2, 4) - 2) + b;
  99. }
  100. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  101. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  102. }
  103. };
  104. ///////////////////////////////////////////////////////////////////////////
  105. // quad
  106. ///////////////////////////////////////////////////////////////////////////
  107. namespace quad {
  108. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  109. return c * pow(t / d, 2) + b;
  110. }
  111. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  112. t = t / d;
  113. return -c * t * (t - 2) + b;
  114. }
  115. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  116. t = t / d * 2;
  117. if (t < 1) return c / 2 * pow(t, 2) + b;
  118. return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
  119. }
  120. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  121. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  122. }
  123. };
  124. ///////////////////////////////////////////////////////////////////////////
  125. // expo
  126. ///////////////////////////////////////////////////////////////////////////
  127. namespace expo {
  128. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  129. if (t == 0) return b;
  130. return c * pow(2, 10 * (t / d - 1)) + b - c * 0.001;
  131. }
  132. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  133. if (t == d) return b + c;
  134. return c * 1.001 * (-pow(2, -10 * t / d) + 1) + b;
  135. }
  136. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  137. if (t == 0) return b;
  138. if (t == d) return b + c;
  139. t = t / d * 2;
  140. if (t < 1) return c / 2 * pow(2, 10 * (t - 1)) + b - c * 0.0005;
  141. return c / 2 * 1.0005 * (-pow(2, -10 * (t - 1)) + 2) + b;
  142. }
  143. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  144. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  145. }
  146. };
  147. ///////////////////////////////////////////////////////////////////////////
  148. // elastic
  149. ///////////////////////////////////////////////////////////////////////////
  150. namespace elastic {
  151. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  152. if (t == 0) return b;
  153. if ((t /= d) == 1) return b + c;
  154. float p = d * 0.3f;
  155. float a = c;
  156. float s = p / 4;
  157. float postFix = a * pow(2, 10 * (t -= 1)); // this is a fix, again, with post-increment operators
  158. return -(postFix * sin((t * d - s) * (2 * pi) / p)) + b;
  159. }
  160. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  161. if (t == 0) return b;
  162. if ((t /= d) == 1) return b + c;
  163. float p = d * 0.3f;
  164. float a = c;
  165. float s = p / 4;
  166. return (a * pow(2, -10 * t) * sin((t * d - s) * (2 * pi) / p) + c + b);
  167. }
  168. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  169. if (t == 0) return b;
  170. if ((t /= d / 2) == 2) return b + c;
  171. float p = d * (0.3f * 1.5f);
  172. float a = c;
  173. float s = p / 4;
  174. if (t < 1) {
  175. float postFix = a * pow(2, 10 * (t -= 1)); // postIncrement is evil
  176. return -0.5f * (postFix * sin((t * d - s) * (2 * pi) / p)) + b;
  177. }
  178. float postFix = a * pow(2, -10 * (t -= 1)); // postIncrement is evil
  179. return postFix * sin((t * d - s) * (2 * pi) / p) * 0.5f + c + b;
  180. }
  181. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  182. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  183. }
  184. };
  185. ///////////////////////////////////////////////////////////////////////////
  186. // cubic
  187. ///////////////////////////////////////////////////////////////////////////
  188. namespace cubic {
  189. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  190. return c * (t /= d) * t * t + b;
  191. }
  192. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  193. t = t / d - 1;
  194. return c * (t * t * t + 1) + b;
  195. }
  196. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  197. if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
  198. return c / 2 * ((t -= 2) * t * t + 2) + b;
  199. }
  200. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  201. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  202. }
  203. };
  204. ///////////////////////////////////////////////////////////////////////////
  205. // circ
  206. ///////////////////////////////////////////////////////////////////////////
  207. namespace circ {
  208. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  209. return -c * (sqrt(1 - (t /= d) * t) - 1) + b; // TODO: ehrich: operation with t is undefined
  210. }
  211. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  212. return c * sqrt(1 - (t = t / d - 1) * t) + b; // TODO: ehrich: operation with t is undefined
  213. }
  214. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  215. if ((t /= d / 2) < 1) return -c / 2 * (sqrt(1 - t * t) - 1) + b;
  216. return c / 2 * (sqrt(1 - t * (t -= 2)) + 1) + b; // TODO: ehrich: operation with t is undefined
  217. }
  218. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  219. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  220. }
  221. };
  222. ///////////////////////////////////////////////////////////////////////////
  223. // bounce
  224. ///////////////////////////////////////////////////////////////////////////
  225. namespace bounce {
  226. static real_t out(real_t t, real_t b, real_t c, real_t d);
  227. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  228. return c - out(d - t, 0, c, d) + b;
  229. }
  230. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  231. if ((t /= d) < (1 / 2.75f)) {
  232. return c * (7.5625f * t * t) + b;
  233. } else if (t < (2 / 2.75f)) {
  234. float postFix = t -= (1.5f / 2.75f);
  235. return c * (7.5625f * (postFix)*t + .75f) + b;
  236. } else if (t < (2.5 / 2.75)) {
  237. float postFix = t -= (2.25f / 2.75f);
  238. return c * (7.5625f * (postFix)*t + .9375f) + b;
  239. } else {
  240. float postFix = t -= (2.625f / 2.75f);
  241. return c * (7.5625f * (postFix)*t + .984375f) + b;
  242. }
  243. }
  244. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  245. return (t < d / 2) ? in(t * 2, b, c / 2, d) : out((t * 2) - d, b + c / 2, c / 2, d);
  246. }
  247. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  248. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  249. }
  250. };
  251. ///////////////////////////////////////////////////////////////////////////
  252. // back
  253. ///////////////////////////////////////////////////////////////////////////
  254. namespace back {
  255. static real_t in(real_t t, real_t b, real_t c, real_t d) {
  256. float s = 1.70158f;
  257. float postFix = t /= d;
  258. return c * (postFix)*t * ((s + 1) * t - s) + b;
  259. }
  260. static real_t out(real_t t, real_t b, real_t c, real_t d) {
  261. float s = 1.70158f;
  262. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; // TODO: ehrich: operation with t is undefined
  263. }
  264. static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
  265. float s = 1.70158f;
  266. if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525f)) + 1) * t - s)) + b; // TODO: ehrich: operation with s is undefined
  267. float postFix = t -= 2;
  268. return c / 2 * ((postFix)*t * (((s *= (1.525f)) + 1) * t + s) + 2) + b; // TODO: ehrich: operation with s is undefined
  269. }
  270. static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
  271. return (t < d / 2) ? out(t * 2, b, c / 2, d) : in((t * 2) - d, b + c / 2, c / 2, d);
  272. }
  273. };
  274. Tween::interpolater Tween::interpolaters[Tween::TRANS_COUNT][Tween::EASE_COUNT] = {
  275. { &linear::in, &linear::out, &linear::in_out, &linear::out_in },
  276. { &sine::in, &sine::out, &sine::in_out, &sine::out_in },
  277. { &quint::in, &quint::out, &quint::in_out, &quint::out_in },
  278. { &quart::in, &quart::out, &quart::in_out, &quart::out_in },
  279. { &quad::in, &quad::out, &quad::in_out, &quad::out_in },
  280. { &expo::in, &expo::out, &expo::in_out, &expo::out_in },
  281. { &elastic::in, &elastic::out, &elastic::in_out, &elastic::out_in },
  282. { &cubic::in, &cubic::out, &cubic::in_out, &cubic::out_in },
  283. { &circ::in, &circ::out, &circ::in_out, &circ::out_in },
  284. { &bounce::in, &bounce::out, &bounce::in_out, &bounce::out_in },
  285. { &back::in, &back::out, &back::in_out, &back::out_in },
  286. };
  287. real_t Tween::_run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d) {
  288. interpolater cb = interpolaters[p_trans_type][p_ease_type];
  289. ERR_FAIL_COND_V(cb == NULL, b);
  290. return cb(t, b, c, d);
  291. }