CCTweenFunction.cpp 11 KB

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