Quaternion.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  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. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Quaternion.h"
  25. const Quaternion Quaternion::sIdentity(1.0f, 0.0f, 0.0f, 0.0f);
  26. Quaternion::Quaternion(float angle, const Vector3& Axis)
  27. {
  28. fromAngleAxis(angle, Axis);
  29. }
  30. Quaternion::Quaternion(float angleX, float angleY, float angleZ)
  31. {
  32. fromEulerAngles(angleX, angleY, angleZ);
  33. }
  34. Quaternion::Quaternion(const Vector3& start, const Vector3& end)
  35. {
  36. fromRotationTo(start, end);
  37. }
  38. void Quaternion::fromAngleAxis(float angle, const Vector3& axis)
  39. {
  40. Vector3 normAxis = axis.getNormalized();
  41. float sinAngle = sinf((angle * M_DEGTORAD) * 0.5f);
  42. float cosAngle = cosf((angle * M_DEGTORAD) * 0.5f);
  43. mW = cosAngle;
  44. mX = normAxis.mX * sinAngle;
  45. mY = normAxis.mY * sinAngle;
  46. mZ = normAxis.mZ * sinAngle;
  47. }
  48. void Quaternion::fromEulerAngles(float angleX, float angleY, float angleZ)
  49. {
  50. // Order of rotations: Z first, then X, then Y (mimics typical FPS camera with gimbal lock at top/bottom)
  51. float sinX = sinf((angleX * M_DEGTORAD) * 0.5f);
  52. float cosX = cosf((angleX * M_DEGTORAD) * 0.5f);
  53. float sinY = sinf((angleY * M_DEGTORAD) * 0.5f);
  54. float cosY = cosf((angleY * M_DEGTORAD) * 0.5f);
  55. float sinZ = sinf((angleZ * M_DEGTORAD) * 0.5f);
  56. float cosZ = cosf((angleZ * M_DEGTORAD) * 0.5f);
  57. mW = cosY * cosX * cosZ + sinY * sinX * sinZ;
  58. mX = cosY * sinX * cosZ + sinY * cosX * sinZ;
  59. mY = sinY * cosX * cosZ - cosY * sinX * sinZ;
  60. mZ = cosY * cosX * sinZ - sinY * sinX * cosZ;
  61. }
  62. void Quaternion::fromRotationTo(const Vector3& start, const Vector3& end)
  63. {
  64. Vector3 normStart = start.getNormalized();
  65. Vector3 normEnd = end.getNormalized();
  66. float d = normStart.dotProduct(normEnd);
  67. if (d > -1.0f + M_EPSILON)
  68. {
  69. Vector3 c = normStart.crossProduct(normEnd);
  70. float s = sqrtf((1.0f + d) * 2.0f);
  71. float invS = 1.0f / s;
  72. mX = c.mX * invS;
  73. mY = c.mY * invS;
  74. mZ = c.mZ * invS;
  75. mW = s * 0.5f;
  76. }
  77. else
  78. {
  79. Vector3 tempAxis = Vector3::sRight.crossProduct(normStart);
  80. if (tempAxis.getLength() < M_EPSILON)
  81. tempAxis = Vector3::sUp.crossProduct(normStart);
  82. fromAngleAxis(180.0f, tempAxis);
  83. }
  84. }
  85. void Quaternion::getEulerAngles(float& angleX, float& angleY, float& angleZ) const
  86. {
  87. // Check for singularities
  88. float check = mX * mY + mZ * mW;
  89. if (check > 0.499f)
  90. {
  91. angleX = 2.0f * atan2f(mX, mW) * M_RADTODEG;
  92. angleY = 90.0f;
  93. angleZ = 0.0f;
  94. return;
  95. }
  96. if (check < -0.499f)
  97. {
  98. angleX = -2.0f * atan2f(mX, mW) * M_RADTODEG;
  99. angleY = -90.0f;
  100. angleZ = 0.0f;
  101. return;
  102. }
  103. float xSquared = mX * mX;
  104. float ySquared = mY * mY;
  105. float zSquared = mZ * mZ;
  106. angleX = atan2f(2.0f * mY * mW - 2.0f * mX * mZ, 1.0f - 2.0f * ySquared - 2.0f * zSquared) * M_RADTODEG;
  107. angleY = asinf(2.0f * check) * M_RADTODEG;
  108. angleZ = atan2f(2.0f * mX * mW - 2.0f * mY * mZ, 1.0f - 2.0f * xSquared - 2.0f * zSquared) * M_RADTODEG;
  109. }
  110. float Quaternion::getYaw() const
  111. {
  112. float x, y, z;
  113. getEulerAngles(x, y, z);
  114. return x;
  115. }
  116. float Quaternion::getPitch() const
  117. {
  118. float x, y, z;
  119. getEulerAngles(x, y, z);
  120. return y;
  121. }
  122. float Quaternion::getRoll() const
  123. {
  124. float x, y, z;
  125. getEulerAngles(x, y, z);
  126. return z;
  127. }
  128. Matrix3 Quaternion::getRotationMatrix() const
  129. {
  130. return Matrix3(
  131. 1.0f - 2.0f * mY * mY - 2.0f * mZ * mZ,
  132. 2.0f * mX * mY - 2.0f * mW * mZ,
  133. 2.0f * mX * mZ + 2.0f * mW * mY,
  134. 2.0f * mX * mY + 2.0f * mW * mZ,
  135. 1.0f - 2.0f * mX * mX - 2.0f * mZ * mZ,
  136. 2.0f * mY * mZ - 2.0f * mW * mX,
  137. 2.0f * mX * mZ - 2.0f * mW * mY,
  138. 2.0f * mY * mZ + 2.0f * mW * mX,
  139. 1.0f - 2.0f * mX * mX - 2.0f * mY * mY
  140. );
  141. }
  142. Quaternion Quaternion::slerp(Quaternion rhs, float t) const
  143. {
  144. float cosAngle = dotProduct(rhs);
  145. // Enable shortest path rotation
  146. if (cosAngle < 0.0f)
  147. {
  148. cosAngle = -cosAngle;
  149. rhs = -rhs;
  150. }
  151. float angle = acosf(cosAngle);
  152. float sinAngle = sinf(angle);
  153. float t1, t2;
  154. if (sinAngle > 0.001f)
  155. {
  156. t1 = sinf((1.0f - t) * angle) / sinAngle;
  157. t2 = sinf(t * angle) / sinAngle;
  158. }
  159. else
  160. {
  161. t1 = 1.0f - t;
  162. t2 = t;
  163. }
  164. return *this * t1 + rhs * t2;
  165. }