2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
22 #include "q_shared.h"
\r
25 angles_t ang_zero( 0.0f, 0.0f, 0.0f );
\r
27 void toAngles( mat3_t &src, angles_t &dst ) {
\r
34 // cap off our sin value so that we don't get any NANs
\r
37 } else if ( sp < -1.0 ) {
\r
41 theta = -asin( sp );
\r
44 if ( cp > 8192 * FLT_EPSILON ) {
\r
45 dst.pitch = theta * 180 / M_PI;
\r
46 dst.yaw = atan2( src[ 0 ][ 1 ], src[ 0 ][ 0 ] ) * 180 / M_PI;
\r
47 dst.roll = atan2( src[ 1 ][ 2 ], src[ 2 ][ 2 ] ) * 180 / M_PI;
\r
49 dst.pitch = theta * 180 / M_PI;
\r
50 dst.yaw = -atan2( src[ 1 ][ 0 ], src[ 1 ][ 1 ] ) * 180 / M_PI;
\r
55 void toAngles( quat_t &src, angles_t &dst ) {
\r
58 toMatrix( src, temp );
\r
59 toAngles( temp, dst );
\r
62 void toAngles( idVec3 &src, angles_t &dst ) {
\r
63 dst.pitch = src[ 0 ];
\r
65 dst.roll = src[ 2 ];
\r
68 void angles_t::toVectors( idVec3 *forward, idVec3 *right, idVec3 *up ) {
\r
70 static float sr, sp, sy, cr, cp, cy; // static to help MS compiler fp bugs
\r
72 angle = yaw * ( M_PI * 2 / 360 );
\r
76 angle = pitch * ( M_PI * 2 / 360 );
\r
80 angle = roll * ( M_PI * 2 / 360 );
\r
85 forward->set( cp * cy, cp * sy, -sp );
\r
89 right->set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );
\r
93 up->set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
\r
97 idVec3 angles_t::toForward( void ) {
\r
99 static float sp, sy, cp, cy; // static to help MS compiler fp bugs
\r
101 angle = yaw * ( M_PI * 2 / 360 );
\r
105 angle = pitch * ( M_PI * 2 / 360 );
\r
109 return idVec3( cp * cy, cp * sy, -sp );
\r
116 returns angles normalized to the range [0 <= angle < 360]
\r
119 angles_t& angles_t::Normalize360( void ) {
\r
120 pitch = (360.0 / 65536) * ( ( int )( pitch * ( 65536 / 360.0 ) ) & 65535 );
\r
121 yaw = (360.0 / 65536) * ( ( int )( yaw * ( 65536 / 360.0 ) ) & 65535 );
\r
122 roll = (360.0 / 65536) * ( ( int )( roll * ( 65536 / 360.0 ) ) & 65535 );
\r
132 returns angles normalized to the range [-180 < angle <= 180]
\r
135 angles_t& angles_t::Normalize180( void ) {
\r
138 if ( pitch > 180.0 ) {
\r
142 if ( yaw > 180.0 ) {
\r
146 if ( roll > 180.0 ) {
\r