mMathFn.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mMathFn.h"
  23. #include "math/mPlane.h"
  24. #include "math/mMatrix.h"
  25. void mTransformPlane(const MatrixF& mat,
  26. const Point3F& scale,
  27. const PlaneF& plane,
  28. PlaneF* result)
  29. {
  30. m_matF_x_scale_x_planeF(mat, &scale.x, &plane.x, &result->x);
  31. }
  32. //--------------------------------------
  33. U32 getNextPow2(U32 io_num)
  34. {
  35. S32 oneCount = 0;
  36. S32 shiftCount = -1;
  37. while (io_num) {
  38. if(io_num & 1)
  39. oneCount++;
  40. shiftCount++;
  41. io_num >>= 1;
  42. }
  43. if(oneCount > 1)
  44. shiftCount++;
  45. return U32(1 << shiftCount);
  46. }
  47. //--------------------------------------
  48. U32 getBinLog2(U32 io_num)
  49. {
  50. AssertFatal(io_num != 0 && isPow2(io_num) == true,
  51. "Error, this only works on powers of 2 > 0");
  52. S32 shiftCount = 0;
  53. while (io_num) {
  54. shiftCount++;
  55. io_num >>= 1;
  56. }
  57. return U32(shiftCount - 1);
  58. }
  59. F32 mEase(const EasingFunction &ease, const F32 &progress)
  60. {
  61. //Animation progress should be between 0 and 1. Result may go past 0 or 1 depending on the animation.
  62. F32 r = mClampF(progress, 0.0f, 1.0f);
  63. switch (ease)
  64. {
  65. case EasingFunction::EaseIn:
  66. r = 1 - mCos((r * M_PI_F) / 2);
  67. break;
  68. case EasingFunction::EaseOut:
  69. r = mSin((r * M_PI_F) / 2);
  70. break;
  71. case EasingFunction::EaseInOut:
  72. r = -(mCos(M_PI_F * r) - 1) / 2;
  73. break;
  74. case EasingFunction::EaseInBack:
  75. r = r * r * ((2.70158 * r) - 1.70158);
  76. break;
  77. case EasingFunction::EaseOutBack:
  78. r = 1 + (--r) * r * ((2.70158 * r) + 1.70158);
  79. break;
  80. case EasingFunction::EaseInOutBack:
  81. if (r < 0.5f)
  82. {
  83. r = r * r * ((7 * r) - 2.5) * 2;
  84. }
  85. else
  86. {
  87. r = 1 + (--r) * r * 2 * ((7 * r) + 2.5);
  88. }
  89. break;
  90. case EasingFunction::EaseInElastic:
  91. if (r < 1 || r > 0)
  92. {
  93. const F32 c = (2 * M_PI_F) / 3;
  94. r = -mPow(2.0f, (10 * r) - 10) * mSin(((r * 10) - 10.75) * c);
  95. }
  96. break;
  97. case EasingFunction::EaseOutElastic:
  98. if (r < 1 || r > 0)
  99. {
  100. const F32 c = (2 * M_PI_F) / 3;
  101. r = (mPow(2.0f, -10 * r) * mSin(((r * 10) - 0.75) * c)) + 1;
  102. }
  103. break;
  104. case EasingFunction::EaseInOutElastic:
  105. if (r < 1 || r > 0)
  106. {
  107. const F32 c = (2 * M_PI_F) / 4.5;
  108. if (r < 0.5f)
  109. {
  110. r = -(mPow(2.0f, (20 * r) - 10) * mSin(((r * 20) - 11.125) * c)) / 2;
  111. }
  112. else
  113. {
  114. r = ((mPow(2.0f, (-20 * r) + 10) * mSin(((r * 20) - 11.125) * c)) / 2) + 1;
  115. }
  116. }
  117. break;
  118. case EasingFunction::EaseInBounce:
  119. r = mPow(2, 6 * (r - 1)) * mFabs(mSin(r * M_PI_F * 3.5f));
  120. break;
  121. case EasingFunction::EaseOutBounce:
  122. r = 1 - mPow(2, -6 * r) * mFabs(mCos(r * M_PI_F * 3.5f));
  123. break;
  124. case EasingFunction::EaseInOutBounce:
  125. if (r < 0.5f)
  126. {
  127. r = 8 * mPow(2, 8 * (r - 1)) * mFabs(mSin(r * M_PI_F * 7.0f));
  128. }
  129. else
  130. {
  131. r = 1 - (8 * mPow(2, -8 * r) * mFabs(mSin(r * M_PI_F * 7.0f)));
  132. }
  133. break;
  134. }
  135. return r;
  136. }