easing_equations.h 10 KB

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