mEase.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. ROBERT PENNER'S MOST EXCELLENT EASING METHODS - ported to Torque C++ by Paul Dana
  3. Easing Equations v1.5
  4. May 1, 2003
  5. (c) 2003 Robert Penner, all rights reserved.
  6. This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
  7. These tweening functions provide different flavors of
  8. math-based motion under a consistent API.
  9. Types of easing:
  10. Linear
  11. Quadratic
  12. Cubic
  13. Quartic
  14. Quintic
  15. Sinusoidal
  16. Exponential
  17. Circular
  18. Elastic
  19. Back
  20. Bounce
  21. Changes:
  22. 1.5 - added bounce easing
  23. 1.4 - added elastic and back easing
  24. 1.3 - tweaked the exponential easing functions to make endpoints exact
  25. 1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
  26. Discussed in Chapter 7 of
  27. Robert Penner's Programming Macromedia Flash MX
  28. (including graphs of the easing equations)
  29. http://www.robertpenner.com/profmx
  30. http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
  31. */
  32. #ifndef _MEASE_H_
  33. #define _MEASE_H_
  34. // the ease methods below all are static and take atomic types as params
  35. // so they are the most generally useful. for convenience, define here
  36. // a type that can contain all the params needed for below to make
  37. // data structures that use these methods cleaner...
  38. //------------------------------------------------------------------------------
  39. class Ease
  40. {
  41. //-------------------------------------- Public data
  42. public:
  43. enum enumDirection
  44. {
  45. InOut=0,
  46. In,
  47. Out
  48. };
  49. enum enumType
  50. {
  51. Linear=0,
  52. Quadratic,
  53. Cubic,
  54. Quartic,
  55. Quintic,
  56. Sinusoidal,
  57. Exponential,
  58. Circular,
  59. Elastic,
  60. Back,
  61. Bounce,
  62. };
  63. };
  64. class EaseF : public Ease
  65. {
  66. //-------------------------------------- Public data
  67. public:
  68. S32 mDir; // inout, in, out
  69. S32 mType; // linear, etc...
  70. F32 mParam[2]; // optional params
  71. //-------------------------------------- Public interface
  72. public:
  73. EaseF();
  74. EaseF(const EaseF &ease);
  75. EaseF(const S32 dir, const S32 type);
  76. EaseF(const S32 dir, const S32 type, F32 param[2]);
  77. //-------------------------------------- Non-math mutators and misc functions
  78. void set(const S32 dir, const S32 type);
  79. void set(const S32 dir, const S32 type, F32 param[2]);
  80. void set(const S32 dir, const S32 type, F32 param0, F32 param1);
  81. void set(const char *s);
  82. F32 getValue(F32 t, F32 b, F32 c, F32 d) const;
  83. F32 getUnitValue(F32 t, bool noExtrapolation) const
  84. {
  85. F32 v = getValue(t,0.0f,1.0f,1.0f);
  86. if (noExtrapolation)
  87. v = mClampF(v,0.0f,1.0f);
  88. return v;
  89. }
  90. F32 getUnitValue(F32 t) const
  91. {
  92. return getValue(t,0.0f,1.0f,1.0f);
  93. }
  94. };
  95. // simple linear tweening - no easing
  96. // t: current time, b: beginning value, c: change in value, d: duration
  97. inline F32 mLinearTween(F32 t, F32 b, F32 c, F32 d)
  98. {
  99. return c*t/d + b;
  100. }
  101. ///////////// QUADRATIC EASING: t^2 ///////////////////
  102. // quadratic easing in - accelerating from zero velocity
  103. // t: current time, b: beginning value, c: change in value, d: duration
  104. // t and d can be in frames or seconds/milliseconds
  105. inline F32 mEaseInQuad(F32 t, F32 b, F32 c, F32 d)
  106. {
  107. t /= d;
  108. return c*t*t + b;
  109. };
  110. // quadratic easing out - decelerating to zero velocity
  111. inline F32 mEaseOutQuad(F32 t, F32 b, F32 c, F32 d)
  112. {
  113. t /= d;
  114. return -c * t*(t-2) + b;
  115. };
  116. // quadratic easing in/out - acceleration until halfway, then deceleration
  117. inline F32 mEaseInOutQuad(F32 t, F32 b, F32 c, F32 d)
  118. {
  119. t /= d/2;
  120. if (t < 1)
  121. return c/2*t*t + b;
  122. t--;
  123. return -c/2 * (t*(t-2) - 1) + b;
  124. };
  125. ///////////// CUBIC EASING: t^3 ///////////////////////
  126. // cubic easing in - accelerating from zero velocity
  127. // t: current time, b: beginning value, c: change in value, d: duration
  128. // t and d can be frames or seconds/milliseconds
  129. inline F32 mEaseInCubic(F32 t, F32 b, F32 c, F32 d)
  130. {
  131. t /= d;
  132. return c*t*t*t + b;
  133. };
  134. // cubic easing out - decelerating to zero velocity
  135. inline F32 mEaseOutCubic(F32 t, F32 b, F32 c, F32 d)
  136. {
  137. t /= d;
  138. t--;
  139. return c*(t*t*t + 1) + b;
  140. };
  141. // cubic easing in/out - acceleration until halfway, then deceleration
  142. inline F32 mEaseInOutCubic(F32 t, F32 b, F32 c, F32 d)
  143. {
  144. t /= d/2;
  145. if (t < 1)
  146. return c/2*t*t*t + b;
  147. t -= 2;
  148. return c/2*(t*t*t + 2) + b;
  149. };
  150. ///////////// QUARTIC EASING: t^4 /////////////////////
  151. // quartic easing in - accelerating from zero velocity
  152. // t: current time, b: beginning value, c: change in value, d: duration
  153. // t and d can be frames or seconds/milliseconds
  154. inline F32 mEaseInQuart(F32 t, F32 b, F32 c, F32 d)
  155. {
  156. t /= d;
  157. return c*t*t*t*t + b;
  158. };
  159. // quartic easing out - decelerating to zero velocity
  160. inline F32 mEaseOutQuart(F32 t, F32 b, F32 c, F32 d)
  161. {
  162. t /= d;
  163. t--;
  164. return -c * (t*t*t*t - 1) + b;
  165. };
  166. // quartic easing in/out - acceleration until halfway, then deceleration
  167. inline F32 mEaseInOutQuart(F32 t, F32 b, F32 c, F32 d)
  168. {
  169. t /= d/2;
  170. if (t < 1)
  171. return c/2*t*t*t*t + b;
  172. t -= 2;
  173. return -c/2 * (t*t*t*t - 2) + b;
  174. };
  175. ///////////// QUINTIC EASING: t^5 ////////////////////
  176. // quintic easing in - accelerating from zero velocity
  177. // t: current time, b: beginning value, c: change in value, d: duration
  178. // t and d can be frames or seconds/milliseconds
  179. inline F32 mEaseInQuint(F32 t, F32 b, F32 c, F32 d)
  180. {
  181. t /= d;
  182. return c*t*t*t*t*t + b;
  183. };
  184. // quintic easing out - decelerating to zero velocity
  185. inline F32 mEaseOutQuint(F32 t, F32 b, F32 c, F32 d)
  186. {
  187. t /= d;
  188. t--;
  189. return c*(t*t*t*t*t + 1) + b;
  190. };
  191. // quintic easing in/out - acceleration until halfway, then deceleration
  192. inline F32 mEaseInOutQuint(F32 t, F32 b, F32 c, F32 d)
  193. {
  194. t /= d/2;
  195. if (t < 1)
  196. return c/2*t*t*t*t*t + b;
  197. t -= 2;
  198. return c/2*(t*t*t*t*t + 2) + b;
  199. };
  200. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  201. // sinusoidal easing in - accelerating from zero velocity
  202. // t: current time, b: beginning value, c: change in position, d: duration
  203. inline F32 mEaseInSine(F32 t, F32 b, F32 c, F32 d)
  204. {
  205. return -c * mCos(t/d * (M_PI_F/2)) + c + b;
  206. };
  207. // sinusoidal easing out - decelerating to zero velocity
  208. inline F32 mEaseOutSine(F32 t, F32 b, F32 c, F32 d)
  209. {
  210. return c * mSin(t/d * (M_PI_F/2)) + b;
  211. };
  212. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  213. inline F32 mEaseInOutSine(F32 t, F32 b, F32 c, F32 d)
  214. {
  215. return -c/2 * (mCos(M_PI_F*t/d) - 1) + b;
  216. };
  217. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  218. // exponential easing in - accelerating from zero velocity
  219. // t: current time, b: beginning value, c: change in position, d: duration
  220. inline F32 mEaseInExpo(F32 t, F32 b, F32 c, F32 d)
  221. {
  222. return c * mPow( 2, 10 * (t/d - 1) ) + b;
  223. };
  224. // exponential easing out - decelerating to zero velocity
  225. inline F32 mEaseOutExpo(F32 t, F32 b, F32 c, F32 d)
  226. {
  227. return c * ( -mPow( 2, -10 * t/d ) + 1 ) + b;
  228. };
  229. // exponential easing in/out - accelerating until halfway, then decelerating
  230. inline F32 mEaseInOutExpo(F32 t, F32 b, F32 c, F32 d)
  231. {
  232. t /= d/2;
  233. if (t < 1)
  234. return c/2 * mPow( 2, 10 * (t - 1) ) + b;
  235. t--;
  236. return c/2 * ( -mPow( 2, -10 * t) + 2 ) + b;
  237. };
  238. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  239. // circular easing in - accelerating from zero velocity
  240. // t: current time, b: beginning value, c: change in position, d: duration
  241. inline F32 mEaseInCirc (F32 t, F32 b, F32 c, F32 d)
  242. {
  243. t/=d;
  244. return -c * (mSqrt(1 - (t)*t) - 1) + b;
  245. };
  246. // circular easing out - decelerating to zero velocity
  247. inline F32 mEaseOutCirc (F32 t, F32 b, F32 c, F32 d)
  248. {
  249. t/=d;
  250. t--;
  251. return c * mSqrt(1 - (t)*t) + b;
  252. };
  253. // circular easing in/out - acceleration until halfway, then deceleration
  254. inline F32 mEaseInOutCirc(F32 t, F32 b, F32 c, F32 d)
  255. {
  256. if ((t/=d/2) < 1)
  257. return -c/2 * (mSqrt(1 - t*t) - 1) + b;
  258. t-=2;
  259. return c/2 * (mSqrt(1 - (t)*t) + 1) + b;
  260. };
  261. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  262. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  263. // t and d can be in frames or seconds/milliseconds
  264. inline F32 mEaseInElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p)
  265. {
  266. if (t==0)
  267. return b;
  268. F32 dt = t /= d;
  269. if (dt == 1)
  270. return b+c;
  271. if (p<=0)
  272. p=d*.3f;
  273. F32 s;
  274. if (a < mFabs(c))
  275. {
  276. a=c;
  277. s=p/4;
  278. }
  279. else
  280. s = p/(2*M_PI_F) * mAsin (c/a);
  281. t -= 1;
  282. return -(a*mPow(2,10*t) * mSin( (t*d-s)*(2*M_PI_F)/p )) + b;
  283. };
  284. inline F32 mEaseOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p)
  285. {
  286. if (t==0)
  287. return b;
  288. F32 dt = t /= d;
  289. if (dt == 1)
  290. return b+c;
  291. if (p<=0)
  292. p=d*.3f;
  293. F32 s;
  294. if (a < mFabs(c))
  295. {
  296. a=c;
  297. s=p/4;
  298. }
  299. else
  300. s = p/(2*M_PI_F) * mAsin (c/a);
  301. return a*mPow(2,-10*t) * mSin( (t*d-s)*(2*M_PI_F)/p ) + c + b;
  302. };
  303. inline F32 mEaseInOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p)
  304. {
  305. if (t==0)
  306. return b;
  307. F32 dt = t /= d / 2;
  308. if (dt == 2)
  309. return b+c;
  310. if (p<=0)
  311. p=d*(.3f*1.5f);
  312. F32 s;
  313. if (a < mFabs(c))
  314. {
  315. a=c;
  316. s=p/4;
  317. }
  318. else
  319. s = p/(2*M_PI_F) * mAsin (c/a);
  320. if (t < 1)
  321. {
  322. t -= 1;
  323. return -.5f*(a*mPow(2, 10 * t) * mSin((t*d - s)*(2 * M_PI_F) / p)) + b;
  324. }
  325. t -= 1;
  326. return a*mPow(2,-10*t) * mSin( (t*d-s)*(2*M_PI_F)/p )*.5f + c + b;
  327. };
  328. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  329. // back easing in - backtracking slightly, then reversing direction and moving to target
  330. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  331. // t and d can be in frames or seconds/milliseconds
  332. // s controls the amount of overshoot: higher s means greater overshoot
  333. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  334. // s==0 produces cubic easing with no overshoot
  335. inline F32 mEaseInBack(F32 t, F32 b, F32 c, F32 d, F32 s)
  336. {
  337. if (s < 0)
  338. s = 1.70158f;
  339. F32 td = t /= d;
  340. return c*td*t*((s + 1)*t - s) + b;
  341. };
  342. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  343. inline F32 mEaseOutBack(F32 t, F32 b, F32 c, F32 d, F32 s)
  344. {
  345. if (s < 0)
  346. s = 1.70158f;
  347. F32 td = t / d - 1;
  348. t = td;
  349. return c*(td*t*((s + 1)*t + s) + 1) + b;
  350. };
  351. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  352. // then overshooting target, reversing, and finally coming back to target
  353. inline F32 mEaseInOutBack(F32 t, F32 b, F32 c, F32 d, F32 s)
  354. {
  355. if (s < 0)
  356. s = 1.70158f;
  357. F32 td = t /= d / 2;
  358. if (td < 1)
  359. {
  360. s *= 1.525f;
  361. return c / 2 * (t*t*((s + 1)*t - s)) + b;
  362. }
  363. s *= 1.525f;
  364. t -= 2;
  365. return c/2*(t*t*((s+1)*t + s) + 2) + b;
  366. };
  367. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  368. // bounce easing out
  369. inline F32 mEaseOutBounce(F32 t, F32 b, F32 c, F32 d)
  370. {
  371. if ((t/=d) < (1/2.75f))
  372. {
  373. return c*(7.5625f*t*t) + b;
  374. }
  375. else if (t < (2/2.75))
  376. {
  377. t -= 1.5f / 2.75f;
  378. return c*(7.5625f*t*t + .75f) + b;
  379. }
  380. else if (t < (2.5/2.75))
  381. {
  382. t -= 2.25f / 2.75f;
  383. return c*(7.5625f*t*t + .9375f) + b;
  384. }
  385. else
  386. {
  387. t -= 2.625f / 2.75f;
  388. return c*(7.5625f*t*t + .984375f) + b;
  389. }
  390. };
  391. // bounce easing in
  392. // t: current time, b: beginning value, c: change in position, d: duration
  393. inline F32 mEaseInBounce(F32 t, F32 b, F32 c, F32 d)
  394. {
  395. return c - mEaseOutBounce (d-t, 0, c, d) + b;
  396. };
  397. // bounce easing in/out
  398. inline F32 mEaseInOutBounce(F32 t, F32 b, F32 c, F32 d)
  399. {
  400. if (t < d/2)
  401. return mEaseInBounce (t*2, 0, c, d) * .5f + b;
  402. return mEaseOutBounce (t*2-d, 0, c, d) * .5f + c*.5f + b;
  403. };
  404. #endif // _MEASE_H_