tween_interpolaters.cpp 13 KB

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