Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

math_angles.cpp

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 #include "q_shared.h"
00023 #include <float.h>
00024 
00025 angles_t ang_zero( 0.0f, 0.0f, 0.0f );
00026 
00027 void toAngles( mat3_t &src, angles_t &dst ) {
00028     double      theta;
00029     double      cp;
00030     double      sp;
00031 
00032     sp = src[ 0 ][ 2 ];
00033 
00034     // cap off our sin value so that we don't get any NANs
00035     if ( sp > 1.0 ) {
00036         sp = 1.0;
00037     } else if ( sp < -1.0 ) {
00038         sp = -1.0;
00039     }
00040 
00041     theta = -asin( sp );
00042     cp = cos( theta );
00043 
00044     if ( cp > 8192 * FLT_EPSILON ) {
00045         dst.pitch   = theta * 180 / M_PI;
00046         dst.yaw     = atan2( src[ 0 ][ 1 ], src[ 0 ][ 0 ] ) * 180 / M_PI;
00047         dst.roll    = atan2( src[ 1 ][ 2 ], src[ 2 ][ 2 ] ) * 180 / M_PI;
00048     } else {
00049         dst.pitch   = theta * 180 / M_PI;
00050         dst.yaw     = -atan2( src[ 1 ][ 0 ], src[ 1 ][ 1 ] ) * 180 / M_PI;
00051         dst.roll    = 0;
00052     }
00053 }
00054 
00055 void toAngles( quat_t &src, angles_t &dst ) {
00056     mat3_t temp;
00057 
00058     toMatrix( src, temp );
00059     toAngles( temp, dst );
00060 }
00061 
00062 void toAngles( idVec3_t &src, angles_t &dst ) {
00063     dst.pitch   = src[ 0 ];
00064     dst.yaw     = src[ 1 ];
00065     dst.roll    = src[ 2 ];
00066 }
00067 
00068 void angles_t::toVectors( idVec3_t *forward, idVec3_t *right, idVec3_t *up ) {
00069     float           angle;
00070     static float    sr, sp, sy, cr, cp, cy; // static to help MS compiler fp bugs
00071     
00072     angle = yaw * ( M_PI * 2 / 360 );
00073     sy = sin( angle );
00074     cy = cos( angle );
00075 
00076     angle = pitch * ( M_PI * 2 / 360 );
00077     sp = sin( angle );
00078     cp = cos( angle );
00079 
00080     angle = roll * ( M_PI * 2 / 360 );
00081     sr = sin( angle );
00082     cr = cos( angle );
00083 
00084     if ( forward ) {
00085         forward->set( cp * cy, cp * sy, -sp );
00086     }
00087 
00088     if ( right ) {
00089         right->set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );
00090     }
00091 
00092     if ( up ) {
00093         up->set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
00094     }
00095 }
00096 
00097 idVec3_t angles_t::toForward( void ) {
00098     float           angle;
00099     static float    sp, sy, cp, cy; // static to help MS compiler fp bugs
00100     
00101     angle = yaw * ( M_PI * 2 / 360 );
00102     sy = sin( angle );
00103     cy = cos( angle );
00104 
00105     angle = pitch * ( M_PI * 2 / 360 );
00106     sp = sin( angle );
00107     cp = cos( angle );
00108 
00109     return idVec3_t( cp * cy, cp * sy, -sp );
00110 }
00111 
00112 /*
00113 =================
00114 Normalize360
00115 
00116 returns angles normalized to the range [0 <= angle < 360]
00117 =================
00118 */
00119 angles_t& angles_t::Normalize360( void ) {
00120     pitch   = (360.0 / 65536) * ( ( int )( pitch    * ( 65536 / 360.0 ) ) & 65535 );
00121     yaw     = (360.0 / 65536) * ( ( int )( yaw      * ( 65536 / 360.0 ) ) & 65535 );
00122     roll    = (360.0 / 65536) * ( ( int )( roll     * ( 65536 / 360.0 ) ) & 65535 );
00123 
00124     return *this;
00125 }
00126 
00127 
00128 /*
00129 =================
00130 Normalize180
00131 
00132 returns angles normalized to the range [-180 < angle <= 180]
00133 =================
00134 */
00135 angles_t& angles_t::Normalize180( void ) {
00136     Normalize360();
00137 
00138     if ( pitch > 180.0 ) {
00139         pitch -= 360.0;
00140     }
00141     
00142     if ( yaw > 180.0 ) {
00143         yaw  -= 360.0;
00144     }
00145 
00146     if ( roll > 180.0 ) {
00147         roll -= 360.0;
00148     }
00149     return *this;
00150 }

Generated on Thu Aug 25 12:37:58 2005 for Quake III Arena by  doxygen 1.3.9.1