home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / math / Rotation.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  2KB  |  131 lines

  1.  
  2. #include "../precompiled.h"
  3. #pragma hdrstop
  4.  
  5.  
  6. /*
  7. ============
  8. idRotation::ToAngles
  9. ============
  10. */
  11. idAngles idRotation::ToAngles( void ) const {
  12.     return ToMat3().ToAngles();
  13. }
  14.  
  15. /*
  16. ============
  17. idRotation::ToQuat
  18. ============
  19. */
  20. idQuat idRotation::ToQuat( void ) const {
  21.     float a, s, c;
  22.  
  23.     a = angle * ( idMath::M_DEG2RAD * 0.5f );
  24.     idMath::SinCos( a, s, c );
  25.     return idQuat( vec.x * s, vec.y * s, vec.z * s, c );
  26. }
  27.  
  28. /*
  29. ============
  30. idRotation::toMat3
  31. ============
  32. */
  33. const idMat3 &idRotation::ToMat3( void ) const {
  34.     float wx, wy, wz;
  35.     float xx, yy, yz;
  36.     float xy, xz, zz;
  37.     float x2, y2, z2;
  38.     float a, c, s, x, y, z;
  39.  
  40.     if ( axisValid ) {
  41.         return axis;
  42.     }
  43.  
  44.     a = angle * ( idMath::M_DEG2RAD * 0.5f );
  45.     idMath::SinCos( a, s, c );
  46.  
  47.     x = vec[0] * s;
  48.     y = vec[1] * s;
  49.     z = vec[2] * s;
  50.  
  51.     x2 = x + x;
  52.     y2 = y + y;
  53.     z2 = z + z;
  54.  
  55.     xx = x * x2;
  56.     xy = x * y2;
  57.     xz = x * z2;
  58.  
  59.     yy = y * y2;
  60.     yz = y * z2;
  61.     zz = z * z2;
  62.  
  63.     wx = c * x2;
  64.     wy = c * y2;
  65.     wz = c * z2;
  66.  
  67.     axis[ 0 ][ 0 ] = 1.0f - ( yy + zz );
  68.     axis[ 0 ][ 1 ] = xy - wz;
  69.     axis[ 0 ][ 2 ] = xz + wy;
  70.  
  71.     axis[ 1 ][ 0 ] = xy + wz;
  72.     axis[ 1 ][ 1 ] = 1.0f - ( xx + zz );
  73.     axis[ 1 ][ 2 ] = yz - wx;
  74.  
  75.     axis[ 2 ][ 0 ] = xz - wy;
  76.     axis[ 2 ][ 1 ] = yz + wx;
  77.     axis[ 2 ][ 2 ] = 1.0f - ( xx + yy );
  78.  
  79.     axisValid = true;
  80.  
  81.     return axis;
  82. }
  83.  
  84. /*
  85. ============
  86. idRotation::ToMat4
  87. ============
  88. */
  89. idMat4 idRotation::ToMat4( void ) const {
  90.     return ToMat3().ToMat4();
  91. }
  92.  
  93. /*
  94. ============
  95. idRotation::ToAngularVelocity
  96. ============
  97. */
  98. idVec3 idRotation::ToAngularVelocity( void ) const {
  99.     return vec * DEG2RAD( angle );
  100. }
  101.  
  102. /*
  103. ============
  104. idRotation::Normalize180
  105. ============
  106. */
  107. void idRotation::Normalize180( void ) {
  108.     angle -= floor( angle / 360.0f ) * 360.0f;
  109.     if ( angle > 180.0f ) {
  110.         angle -= 360.0f;
  111.     }
  112.     else if ( angle < -180.0f ) {
  113.         angle += 360.0f;
  114.     }
  115. }
  116.  
  117. /*
  118. ============
  119. idRotation::Normalize360
  120. ============
  121. */
  122. void idRotation::Normalize360( void ) {
  123.     angle -= floor( angle / 360.0f ) * 360.0f;
  124.     if ( angle > 360.0f ) {
  125.         angle -= 360.0f;
  126.     }
  127.     else if ( angle < 0.0f ) {
  128.         angle += 360.0f;
  129.     }
  130. }
  131.