DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Rotation.cpp (3569B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 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.
     23 
     24 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.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #pragma hdrstop
     30 #include "../precompiled.h"
     31 
     32 /*
     33 ============
     34 idRotation::ToAngles
     35 ============
     36 */
     37 idAngles idRotation::ToAngles() const {
     38 	return ToMat3().ToAngles();
     39 }
     40 
     41 /*
     42 ============
     43 idRotation::ToQuat
     44 ============
     45 */
     46 idQuat idRotation::ToQuat() const {
     47 	float a, s, c;
     48 
     49 	a = angle * ( idMath::M_DEG2RAD * 0.5f );
     50 	idMath::SinCos( a, s, c );
     51 	return idQuat( vec.x * s, vec.y * s, vec.z * s, c );
     52 }
     53 
     54 /*
     55 ============
     56 idRotation::toMat3
     57 ============
     58 */
     59 const idMat3 &idRotation::ToMat3() const {
     60 	float wx, wy, wz;
     61 	float xx, yy, yz;
     62 	float xy, xz, zz;
     63 	float x2, y2, z2;
     64 	float a, c, s, x, y, z;
     65 
     66 	if ( axisValid ) {
     67 		return axis;
     68 	}
     69 
     70 	a = angle * ( idMath::M_DEG2RAD * 0.5f );
     71 	idMath::SinCos( a, s, c );
     72 
     73 	x = vec[0] * s;
     74 	y = vec[1] * s;
     75 	z = vec[2] * s;
     76 
     77 	x2 = x + x;
     78 	y2 = y + y;
     79 	z2 = z + z;
     80 
     81 	xx = x * x2;
     82 	xy = x * y2;
     83 	xz = x * z2;
     84 
     85 	yy = y * y2;
     86 	yz = y * z2;
     87 	zz = z * z2;
     88 
     89 	wx = c * x2;
     90 	wy = c * y2;
     91 	wz = c * z2;
     92 
     93 	axis[ 0 ][ 0 ] = 1.0f - ( yy + zz );
     94 	axis[ 0 ][ 1 ] = xy - wz;
     95 	axis[ 0 ][ 2 ] = xz + wy;
     96 
     97 	axis[ 1 ][ 0 ] = xy + wz;
     98 	axis[ 1 ][ 1 ] = 1.0f - ( xx + zz );
     99 	axis[ 1 ][ 2 ] = yz - wx;
    100 
    101 	axis[ 2 ][ 0 ] = xz - wy;
    102 	axis[ 2 ][ 1 ] = yz + wx;
    103 	axis[ 2 ][ 2 ] = 1.0f - ( xx + yy );
    104 
    105 	axisValid = true;
    106 
    107 	return axis;
    108 }
    109 
    110 /*
    111 ============
    112 idRotation::ToMat4
    113 ============
    114 */
    115 idMat4 idRotation::ToMat4() const {
    116 	return ToMat3().ToMat4();
    117 }
    118 
    119 /*
    120 ============
    121 idRotation::ToAngularVelocity
    122 ============
    123 */
    124 idVec3 idRotation::ToAngularVelocity() const {
    125 	return vec * DEG2RAD( angle );
    126 }
    127 
    128 /*
    129 ============
    130 idRotation::Normalize180
    131 ============
    132 */
    133 void idRotation::Normalize180() {
    134 	angle -= floor( angle / 360.0f ) * 360.0f;
    135 	if ( angle > 180.0f ) {
    136 		angle -= 360.0f;
    137 	}
    138 	else if ( angle < -180.0f ) {
    139 		angle += 360.0f;
    140 	}
    141 }
    142 
    143 /*
    144 ============
    145 idRotation::Normalize360
    146 ============
    147 */
    148 void idRotation::Normalize360() {
    149 	angle -= floor( angle / 360.0f ) * 360.0f;
    150 	if ( angle > 360.0f ) {
    151 		angle -= 360.0f;
    152 	}
    153 	else if ( angle < 0.0f ) {
    154 		angle += 360.0f;
    155 	}
    156 }