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

  1. #include "../precompiled.h"
  2. #pragma hdrstop
  3.  
  4. #include <float.h>
  5.  
  6. /*
  7. =================
  8. rvAngles::NormalizeFull
  9.  
  10. returns angles normalized to the range [0 <= angle < TWO_PI]
  11. =================
  12. */
  13. rvAngles &rvAngles::NormalizeFull( void ) 
  14. {
  15.     // Get to -TWO_PI < a < TWO_PI
  16.     pitch = fmodf( pitch, idMath::TWO_PI );
  17.     yaw = fmodf( yaw, idMath::TWO_PI );
  18.     roll = fmodf( roll, idMath::TWO_PI );
  19.  
  20.     // Fix any negatives
  21.     if( pitch < 0.0f )
  22.     {
  23.         pitch += idMath::TWO_PI;
  24.     }
  25.  
  26.     if( yaw < 0.0f )
  27.     {
  28.         yaw += idMath::TWO_PI;
  29.     }
  30.  
  31.     if( roll < 0.0f )
  32.     {
  33.         roll += idMath::TWO_PI;
  34.     }
  35.  
  36.     return( *this );
  37. }
  38.  
  39. /*
  40. =================
  41. rvAngles::NormalizeHalf
  42.  
  43. returns angles normalized to the range [-PI < angle <= PI]
  44. =================
  45. */
  46. rvAngles &rvAngles::NormalizeHalf( void ) 
  47. {
  48.     int        i;
  49.  
  50.     // Get to -TWO_PI < a < TWO_PI
  51.     pitch = fmodf( pitch, idMath::TWO_PI );
  52.     yaw = fmodf( yaw, idMath::TWO_PI );
  53.     roll = fmodf( roll, idMath::TWO_PI );
  54.  
  55.     for( i = 0; i < 3; i++ )
  56.     {
  57.         if( ( *this )[i] < -idMath::PI )
  58.         {
  59.             ( *this )[i] += idMath::TWO_PI;
  60.         }
  61.  
  62.         if( ( *this )[i] > idMath::PI )
  63.         {
  64.             ( *this )[i] -= idMath::TWO_PI;
  65.         }
  66.     }
  67.  
  68.     return( *this );
  69. }
  70.  
  71. /*
  72. =================
  73. rvAngles::ToVectors
  74. =================
  75. */
  76. void rvAngles::ToVectors( idVec3 *forward, idVec3 *right, idVec3 *up ) const 
  77. {
  78.     float    sr, sp, sy, cr, cp, cy;
  79.     
  80.     idMath::SinCos( yaw, sy, cy );
  81.     idMath::SinCos( pitch, sp, cp );
  82.     idMath::SinCos( roll, sr, cr );
  83.  
  84.     if( forward ) 
  85.     {
  86.         forward->Set( cp * cy, cp * sy, -sp );
  87.     }
  88.  
  89.     if( right ) 
  90.     {
  91.         right->Set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );
  92.     }
  93.  
  94.     if( up ) 
  95.     {
  96.         up->Set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
  97.     }
  98. }
  99.  
  100. /*
  101. =================
  102. rvAngles::ToForward
  103. =================
  104. */
  105. idVec3 rvAngles::ToForward( void ) const 
  106. {
  107.     float    sp, sy, cp, cy;
  108.     
  109.     idMath::SinCos( yaw, sy, cy );
  110.     idMath::SinCos( pitch, sp, cp );
  111.  
  112.     return( idVec3( cp * cy, cp * sy, -sp ) );
  113. }
  114.  
  115. /*
  116. =================
  117. rvAngles::ToMat3
  118. =================
  119. */
  120. idMat3 &rvAngles::ToMat3( idMat3 &mat ) const 
  121. {
  122.     float    sr, sp, sy, cr, cp, cy;
  123.         
  124.     idMath::SinCos( yaw, sy, cy );
  125.     idMath::SinCos( pitch, sp, cp );
  126.     idMath::SinCos( roll, sr, cr );
  127.  
  128.     mat[0].Set( cp * cy, cp * sy, -sp );
  129.     mat[1].Set( sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp );
  130.     mat[2].Set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
  131.  
  132.     return( mat );
  133. }
  134.  
  135. // end
  136.