easing_equations.h 11 KB

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