Rotation.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../precompiled.h"
  22. /*
  23. ============
  24. idRotation::ToAngles
  25. ============
  26. */
  27. idAngles idRotation::ToAngles() const {
  28. return ToMat3().ToAngles();
  29. }
  30. /*
  31. ============
  32. idRotation::ToQuat
  33. ============
  34. */
  35. idQuat idRotation::ToQuat() const {
  36. float a, s, c;
  37. a = angle * ( idMath::M_DEG2RAD * 0.5f );
  38. idMath::SinCos( a, s, c );
  39. return idQuat( vec.x * s, vec.y * s, vec.z * s, c );
  40. }
  41. /*
  42. ============
  43. idRotation::toMat3
  44. ============
  45. */
  46. const idMat3 &idRotation::ToMat3() const {
  47. float wx, wy, wz;
  48. float xx, yy, yz;
  49. float xy, xz, zz;
  50. float x2, y2, z2;
  51. float a, c, s, x, y, z;
  52. if ( axisValid ) {
  53. return axis;
  54. }
  55. a = angle * ( idMath::M_DEG2RAD * 0.5f );
  56. idMath::SinCos( a, s, c );
  57. x = vec[0] * s;
  58. y = vec[1] * s;
  59. z = vec[2] * s;
  60. x2 = x + x;
  61. y2 = y + y;
  62. z2 = z + z;
  63. xx = x * x2;
  64. xy = x * y2;
  65. xz = x * z2;
  66. yy = y * y2;
  67. yz = y * z2;
  68. zz = z * z2;
  69. wx = c * x2;
  70. wy = c * y2;
  71. wz = c * z2;
  72. axis[ 0 ][ 0 ] = 1.0f - ( yy + zz );
  73. axis[ 0 ][ 1 ] = xy - wz;
  74. axis[ 0 ][ 2 ] = xz + wy;
  75. axis[ 1 ][ 0 ] = xy + wz;
  76. axis[ 1 ][ 1 ] = 1.0f - ( xx + zz );
  77. axis[ 1 ][ 2 ] = yz - wx;
  78. axis[ 2 ][ 0 ] = xz - wy;
  79. axis[ 2 ][ 1 ] = yz + wx;
  80. axis[ 2 ][ 2 ] = 1.0f - ( xx + yy );
  81. axisValid = true;
  82. return axis;
  83. }
  84. /*
  85. ============
  86. idRotation::ToMat4
  87. ============
  88. */
  89. idMat4 idRotation::ToMat4() const {
  90. return ToMat3().ToMat4();
  91. }
  92. /*
  93. ============
  94. idRotation::ToAngularVelocity
  95. ============
  96. */
  97. idVec3 idRotation::ToAngularVelocity() const {
  98. return vec * DEG2RAD( angle );
  99. }
  100. /*
  101. ============
  102. idRotation::Normalize180
  103. ============
  104. */
  105. void idRotation::Normalize180() {
  106. angle -= floor( angle / 360.0f ) * 360.0f;
  107. if ( angle > 180.0f ) {
  108. angle -= 360.0f;
  109. }
  110. else if ( angle < -180.0f ) {
  111. angle += 360.0f;
  112. }
  113. }
  114. /*
  115. ============
  116. idRotation::Normalize360
  117. ============
  118. */
  119. void idRotation::Normalize360() {
  120. angle -= floor( angle / 360.0f ) * 360.0f;
  121. if ( angle > 360.0f ) {
  122. angle -= 360.0f;
  123. }
  124. else if ( angle < 0.0f ) {
  125. angle += 360.0f;
  126. }
  127. }