Angles.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include <float.h>
  23. idAngles ang_zero( 0.0f, 0.0f, 0.0f );
  24. /*
  25. =================
  26. idAngles::Normalize360
  27. returns angles normalized to the range [0 <= angle < 360]
  28. =================
  29. */
  30. idAngles& idAngles::Normalize360() {
  31. int i;
  32. for ( i = 0; i < 3; i++ ) {
  33. if ( ( (*this)[i] >= 360.0f ) || ( (*this)[i] < 0.0f ) ) {
  34. (*this)[i] -= floor( (*this)[i] / 360.0f ) * 360.0f;
  35. if ( (*this)[i] >= 360.0f ) {
  36. (*this)[i] -= 360.0f;
  37. }
  38. if ( (*this)[i] < 0.0f ) {
  39. (*this)[i] += 360.0f;
  40. }
  41. }
  42. }
  43. return *this;
  44. }
  45. /*
  46. =================
  47. idAngles::Normalize180
  48. returns angles normalized to the range [-180 < angle <= 180]
  49. =================
  50. */
  51. idAngles& idAngles::Normalize180() {
  52. Normalize360();
  53. if ( pitch > 180.0f ) {
  54. pitch -= 360.0f;
  55. }
  56. if ( yaw > 180.0f ) {
  57. yaw -= 360.0f;
  58. }
  59. if ( roll > 180.0f ) {
  60. roll -= 360.0f;
  61. }
  62. return *this;
  63. }
  64. /*
  65. =================
  66. idAngles::ToVectors
  67. =================
  68. */
  69. void idAngles::ToVectors( idVec3 *forward, idVec3 *right, idVec3 *up ) const {
  70. float sr, sp, sy, cr, cp, cy;
  71. idMath::SinCos( DEG2RAD( yaw ), sy, cy );
  72. idMath::SinCos( DEG2RAD( pitch ), sp, cp );
  73. idMath::SinCos( DEG2RAD( roll ), sr, cr );
  74. if ( forward ) {
  75. forward->Set( cp * cy, cp * sy, -sp );
  76. }
  77. if ( right ) {
  78. right->Set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );
  79. }
  80. if ( up ) {
  81. up->Set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
  82. }
  83. }
  84. /*
  85. =================
  86. idAngles::ToForward
  87. =================
  88. */
  89. idVec3 idAngles::ToForward() const {
  90. float sp, sy, cp, cy;
  91. idMath::SinCos( DEG2RAD( yaw ), sy, cy );
  92. idMath::SinCos( DEG2RAD( pitch ), sp, cp );
  93. return idVec3( cp * cy, cp * sy, -sp );
  94. }
  95. /*
  96. =================
  97. idAngles::ToQuat
  98. =================
  99. */
  100. idQuat idAngles::ToQuat() const {
  101. float sx, cx, sy, cy, sz, cz;
  102. float sxcy, cxcy, sxsy, cxsy;
  103. idMath::SinCos( DEG2RAD( yaw ) * 0.5f, sz, cz );
  104. idMath::SinCos( DEG2RAD( pitch ) * 0.5f, sy, cy );
  105. idMath::SinCos( DEG2RAD( roll ) * 0.5f, sx, cx );
  106. sxcy = sx * cy;
  107. cxcy = cx * cy;
  108. sxsy = sx * sy;
  109. cxsy = cx * sy;
  110. return idQuat( cxsy*sz - sxcy*cz, -cxsy*cz - sxcy*sz, sxsy*cz - cxcy*sz, cxcy*cz + sxsy*sz );
  111. }
  112. /*
  113. =================
  114. idAngles::ToRotation
  115. =================
  116. */
  117. idRotation idAngles::ToRotation() const {
  118. idVec3 vec;
  119. float angle, w;
  120. float sx, cx, sy, cy, sz, cz;
  121. float sxcy, cxcy, sxsy, cxsy;
  122. if ( pitch == 0.0f ) {
  123. if ( yaw == 0.0f ) {
  124. return idRotation( vec3_origin, idVec3( -1.0f, 0.0f, 0.0f ), roll );
  125. }
  126. if ( roll == 0.0f ) {
  127. return idRotation( vec3_origin, idVec3( 0.0f, 0.0f, -1.0f ), yaw );
  128. }
  129. } else if ( yaw == 0.0f && roll == 0.0f ) {
  130. return idRotation( vec3_origin, idVec3( 0.0f, -1.0f, 0.0f ), pitch );
  131. }
  132. idMath::SinCos( DEG2RAD( yaw ) * 0.5f, sz, cz );
  133. idMath::SinCos( DEG2RAD( pitch ) * 0.5f, sy, cy );
  134. idMath::SinCos( DEG2RAD( roll ) * 0.5f, sx, cx );
  135. sxcy = sx * cy;
  136. cxcy = cx * cy;
  137. sxsy = sx * sy;
  138. cxsy = cx * sy;
  139. vec.x = cxsy * sz - sxcy * cz;
  140. vec.y = -cxsy * cz - sxcy * sz;
  141. vec.z = sxsy * cz - cxcy * sz;
  142. w = cxcy * cz + sxsy * sz;
  143. angle = idMath::ACos( w );
  144. if ( angle == 0.0f ) {
  145. vec.Set( 0.0f, 0.0f, 1.0f );
  146. } else {
  147. //vec *= (1.0f / sin( angle ));
  148. vec.Normalize();
  149. vec.FixDegenerateNormal();
  150. angle *= 2.0f * idMath::M_RAD2DEG;
  151. }
  152. return idRotation( vec3_origin, vec, angle );
  153. }
  154. /*
  155. =================
  156. idAngles::ToMat3
  157. =================
  158. */
  159. idMat3 idAngles::ToMat3() const {
  160. idMat3 mat;
  161. float sr, sp, sy, cr, cp, cy;
  162. idMath::SinCos( DEG2RAD( yaw ), sy, cy );
  163. idMath::SinCos( DEG2RAD( pitch ), sp, cp );
  164. idMath::SinCos( DEG2RAD( roll ), sr, cr );
  165. mat[ 0 ].Set( cp * cy, cp * sy, -sp );
  166. mat[ 1 ].Set( sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp );
  167. mat[ 2 ].Set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
  168. return mat;
  169. }
  170. /*
  171. =================
  172. idAngles::ToMat4
  173. =================
  174. */
  175. idMat4 idAngles::ToMat4() const {
  176. return ToMat3().ToMat4();
  177. }
  178. /*
  179. =================
  180. idAngles::ToAngularVelocity
  181. =================
  182. */
  183. idVec3 idAngles::ToAngularVelocity() const {
  184. idRotation rotation = idAngles::ToRotation();
  185. return rotation.GetVec() * DEG2RAD( rotation.GetAngle() );
  186. }
  187. /*
  188. =============
  189. idAngles::ToString
  190. =============
  191. */
  192. const char *idAngles::ToString( int precision ) const {
  193. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  194. }