CCTweenFunction.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "2d/CCTweenFunction.h"
  22. #define _USE_MATH_DEFINES // needed for M_PI and M_PI2
  23. #include <math.h> // M_PI
  24. #undef _USE_MATH_DEFINES
  25. NS_CC_BEGIN
  26. namespace tweenfunc {
  27. #ifndef M_PI_X_2
  28. #define M_PI_X_2 (float)M_PI * 2.0f
  29. #endif
  30. float tweenTo(float time, TweenType type, float *easingParam)
  31. {
  32. float delta = 0;
  33. switch (type)
  34. {
  35. case CUSTOM_EASING:
  36. delta = customEase(time, easingParam);
  37. break;
  38. case Linear:
  39. delta = linear(time);
  40. break;
  41. case Sine_EaseIn:
  42. delta = sineEaseIn(time);
  43. break;
  44. case Sine_EaseOut:
  45. delta = sineEaseOut(time);
  46. break;
  47. case Sine_EaseInOut:
  48. delta = sineEaseInOut(time);
  49. break;
  50. case Quad_EaseIn:
  51. delta = quadEaseIn(time);
  52. break;
  53. case Quad_EaseOut:
  54. delta = quadEaseOut(time);
  55. break;
  56. case Quad_EaseInOut:
  57. delta = quadEaseInOut(time);
  58. break;
  59. case Cubic_EaseIn:
  60. delta = cubicEaseIn(time);
  61. break;
  62. case Cubic_EaseOut:
  63. delta = cubicEaseOut(time);
  64. break;
  65. case Cubic_EaseInOut:
  66. delta = cubicEaseInOut(time);
  67. break;
  68. case Quart_EaseIn:
  69. delta = quartEaseIn(time);
  70. break;
  71. case Quart_EaseOut:
  72. delta = quartEaseOut(time);
  73. break;
  74. case Quart_EaseInOut:
  75. delta = quartEaseInOut(time);
  76. break;
  77. case Quint_EaseIn:
  78. delta = quintEaseIn(time);
  79. break;
  80. case Quint_EaseOut:
  81. delta = quintEaseOut(time);
  82. break;
  83. case Quint_EaseInOut:
  84. delta = quintEaseInOut(time);
  85. break;
  86. case Expo_EaseIn:
  87. delta = expoEaseIn(time);
  88. break;
  89. case Expo_EaseOut:
  90. delta = expoEaseOut(time);
  91. break;
  92. case Expo_EaseInOut:
  93. delta = expoEaseInOut(time);
  94. break;
  95. case Circ_EaseIn:
  96. delta = circEaseIn(time);
  97. break;
  98. case Circ_EaseOut:
  99. delta = circEaseOut(time);
  100. break;
  101. case Circ_EaseInOut:
  102. delta = circEaseInOut(time);
  103. break;
  104. case Elastic_EaseIn:
  105. {
  106. float period = 0.3f;
  107. if (nullptr != easingParam) {
  108. period = easingParam[0];
  109. }
  110. delta = elasticEaseIn(time, period);
  111. }
  112. break;
  113. case Elastic_EaseOut:
  114. {
  115. float period = 0.3f;
  116. if (nullptr != easingParam) {
  117. period = easingParam[0];
  118. }
  119. delta = elasticEaseOut(time, period);
  120. }
  121. break;
  122. case Elastic_EaseInOut:
  123. {
  124. float period = 0.3f;
  125. if (nullptr != easingParam) {
  126. period = easingParam[0];
  127. }
  128. delta = elasticEaseInOut(time, period);
  129. }
  130. break;
  131. case Back_EaseIn:
  132. delta = backEaseIn(time);
  133. break;
  134. case Back_EaseOut:
  135. delta = backEaseOut(time);
  136. break;
  137. case Back_EaseInOut:
  138. delta = backEaseInOut(time);
  139. break;
  140. case Bounce_EaseIn:
  141. delta = bounceEaseIn(time);
  142. break;
  143. case Bounce_EaseOut:
  144. delta = bounceEaseOut(time);
  145. break;
  146. case Bounce_EaseInOut:
  147. delta = bounceEaseInOut(time);
  148. break;
  149. default:
  150. delta = sineEaseInOut(time);
  151. break;
  152. }
  153. return delta;
  154. }
  155. // Linear
  156. float linear(float time)
  157. {
  158. return time;
  159. }
  160. // Sine Ease
  161. float sineEaseIn(float time)
  162. {
  163. return -1 * cosf(time * (float)M_PI_2) + 1;
  164. }
  165. float sineEaseOut(float time)
  166. {
  167. return sinf(time * (float)M_PI_2);
  168. }
  169. float sineEaseInOut(float time)
  170. {
  171. return -0.5f * (cosf((float)M_PI * time) - 1);
  172. }
  173. // Quad Ease
  174. float quadEaseIn(float time)
  175. {
  176. return time * time;
  177. }
  178. float quadEaseOut(float time)
  179. {
  180. return -1 * time * (time - 2);
  181. }
  182. float quadEaseInOut(float time)
  183. {
  184. time = time*2;
  185. if (time < 1)
  186. return 0.5f * time * time;
  187. --time;
  188. return -0.5f * (time * (time - 2) - 1);
  189. }
  190. // Cubic Ease
  191. float cubicEaseIn(float time)
  192. {
  193. return time * time * time;
  194. }
  195. float cubicEaseOut(float time)
  196. {
  197. time -= 1;
  198. return (time * time * time + 1);
  199. }
  200. float cubicEaseInOut(float time)
  201. {
  202. time = time*2;
  203. if (time < 1)
  204. return 0.5f * time * time * time;
  205. time -= 2;
  206. return 0.5f * (time * time * time + 2);
  207. }
  208. // Quart Ease
  209. float quartEaseIn(float time)
  210. {
  211. return time * time * time * time;
  212. }
  213. float quartEaseOut(float time)
  214. {
  215. time -= 1;
  216. return -(time * time * time * time - 1);
  217. }
  218. float quartEaseInOut(float time)
  219. {
  220. time = time*2;
  221. if (time < 1)
  222. return 0.5f * time * time * time * time;
  223. time -= 2;
  224. return -0.5f * (time * time * time * time - 2);
  225. }
  226. // Quint Ease
  227. float quintEaseIn(float time)
  228. {
  229. return time * time * time * time * time;
  230. }
  231. float quintEaseOut(float time)
  232. {
  233. time -=1;
  234. return (time * time * time * time * time + 1);
  235. }
  236. float quintEaseInOut(float time)
  237. {
  238. time = time*2;
  239. if (time < 1)
  240. return 0.5f * time * time * time * time * time;
  241. time -= 2;
  242. return 0.5f * (time * time * time * time * time + 2);
  243. }
  244. // Expo Ease
  245. float expoEaseIn(float time)
  246. {
  247. return time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f;
  248. }
  249. float expoEaseOut(float time)
  250. {
  251. return time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1);
  252. }
  253. float expoEaseInOut(float time)
  254. {
  255. if(time == 0 || time == 1)
  256. return time;
  257. if (time < 0.5f)
  258. return 0.5f * powf(2, 10 * (time * 2 - 1));
  259. return 0.5f * (-powf(2, -10 * (time * 2 - 1)) + 2);
  260. }
  261. // Circ Ease
  262. float circEaseIn(float time)
  263. {
  264. return -1 * (sqrt(1 - time * time) - 1);
  265. }
  266. float circEaseOut(float time)
  267. {
  268. time = time - 1;
  269. return sqrt(1 - time * time);
  270. }
  271. float circEaseInOut(float time)
  272. {
  273. time = time * 2;
  274. if (time < 1)
  275. return -0.5f * (sqrt(1 - time * time) - 1);
  276. time -= 2;
  277. return 0.5f * (sqrt(1 - time * time) + 1);
  278. }
  279. // Elastic Ease
  280. float elasticEaseIn(float time, float period)
  281. {
  282. float newT = 0;
  283. if (time == 0 || time == 1)
  284. {
  285. newT = time;
  286. }
  287. else
  288. {
  289. float s = period / 4;
  290. time = time - 1;
  291. newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / period);
  292. }
  293. return newT;
  294. }
  295. float elasticEaseOut(float time, float period)
  296. {
  297. float newT = 0;
  298. if (time == 0 || time == 1)
  299. {
  300. newT = time;
  301. }
  302. else
  303. {
  304. float s = period / 4;
  305. newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) + 1;
  306. }
  307. return newT;
  308. }
  309. float elasticEaseInOut(float time, float period)
  310. {
  311. float newT = 0;
  312. if (time == 0 || time == 1)
  313. {
  314. newT = time;
  315. }
  316. else
  317. {
  318. time = time * 2;
  319. if (! period)
  320. {
  321. period = 0.3f * 1.5f;
  322. }
  323. float s = period / 4;
  324. time = time - 1;
  325. if (time < 0)
  326. {
  327. newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / period);
  328. }
  329. else
  330. {
  331. newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) * 0.5f + 1;
  332. }
  333. }
  334. return newT;
  335. }
  336. // Back Ease
  337. float backEaseIn(float time)
  338. {
  339. float overshoot = 1.70158f;
  340. return time * time * ((overshoot + 1) * time - overshoot);
  341. }
  342. float backEaseOut(float time)
  343. {
  344. float overshoot = 1.70158f;
  345. time = time - 1;
  346. return time * time * ((overshoot + 1) * time + overshoot) + 1;
  347. }
  348. float backEaseInOut(float time)
  349. {
  350. float overshoot = 1.70158f * 1.525f;
  351. time = time * 2;
  352. if (time < 1)
  353. {
  354. return (time * time * ((overshoot + 1) * time - overshoot)) / 2;
  355. }
  356. else
  357. {
  358. time = time - 2;
  359. return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1;
  360. }
  361. }
  362. // Bounce Ease
  363. float bounceTime(float time)
  364. {
  365. if (time < 1 / 2.75f)
  366. {
  367. return 7.5625f * time * time;
  368. }
  369. else if (time < 2 / 2.75f)
  370. {
  371. time -= 1.5f / 2.75f;
  372. return 7.5625f * time * time + 0.75f;
  373. }
  374. else if(time < 2.5f / 2.75f)
  375. {
  376. time -= 2.25f / 2.75f;
  377. return 7.5625f * time * time + 0.9375f;
  378. }
  379. time -= 2.625f / 2.75f;
  380. return 7.5625f * time * time + 0.984375f;
  381. }
  382. float bounceEaseIn(float time)
  383. {
  384. return 1 - bounceTime(1 - time);
  385. }
  386. float bounceEaseOut(float time)
  387. {
  388. return bounceTime(time);
  389. }
  390. float bounceEaseInOut(float time)
  391. {
  392. float newT = 0;
  393. if (time < 0.5f)
  394. {
  395. time = time * 2;
  396. newT = (1 - bounceTime(1 - time)) * 0.5f;
  397. }
  398. else
  399. {
  400. newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f;
  401. }
  402. return newT;
  403. }
  404. // Custom Ease
  405. float customEase(float time, float *easingParam)
  406. {
  407. if (easingParam)
  408. {
  409. float tt = 1-time;
  410. return easingParam[1]*tt*tt*tt + 3*easingParam[3]*time*tt*tt + 3*easingParam[5]*time*time*tt + easingParam[7]*time*time*time;
  411. }
  412. return time;
  413. }
  414. float easeIn(float time, float rate)
  415. {
  416. return powf(time, rate);
  417. }
  418. float easeOut(float time, float rate)
  419. {
  420. return powf(time, 1 / rate);
  421. }
  422. float easeInOut(float time, float rate)
  423. {
  424. time *= 2;
  425. if (time < 1)
  426. {
  427. return 0.5f * powf(time, rate);
  428. }
  429. else
  430. {
  431. return (1.0f - 0.5f * powf(2 - time, rate));
  432. }
  433. }
  434. float quadraticIn(float time)
  435. {
  436. return powf(time,2);
  437. }
  438. float quadraticOut(float time)
  439. {
  440. return -time*(time-2);
  441. }
  442. float quadraticInOut(float time)
  443. {
  444. float resultTime = time;
  445. time = time*2;
  446. if (time < 1)
  447. {
  448. resultTime = time * time * 0.5f;
  449. }
  450. else
  451. {
  452. --time;
  453. resultTime = -0.5f * (time * (time - 2) - 1);
  454. }
  455. return resultTime;
  456. }
  457. float bezieratFunction( float a, float b, float c, float d, float t )
  458. {
  459. return (powf(1-t,3) * a + 3*t*(powf(1-t,2))*b + 3*powf(t,2)*(1-t)*c + powf(t,3)*d );
  460. }
  461. }
  462. NS_CC_END