math_vector.cpp (3060B)
1 /* 2 =========================================================================== 3 Copyright (C) 1999-2005 Id Software, Inc. 4 5 This file is part of Quake III Arena source code. 6 7 Quake III Arena source code is free software; you can redistribute it 8 and/or modify it under the terms of the GNU General Public License as 9 published by the Free Software Foundation; either version 2 of the License, 10 or (at your option) any later version. 11 12 Quake III Arena source code is distributed in the hope that it will be 13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with Foobar; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 =========================================================================== 21 */ 22 //#include "../game/q_shared.h" 23 #include "math_vector.h" 24 #include <assert.h> 25 #include <math.h> 26 #include <stdio.h> 27 #include <stdarg.h> 28 #include <string.h> 29 #include <stdlib.h> 30 #include <time.h> 31 #include <ctype.h> 32 33 #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h 34 35 #define LERP_DELTA 1e-6 36 37 idVec3_t vec_zero( 0.0f, 0.0f, 0.0f ); 38 39 Bounds boundsZero; 40 41 float idVec3_t::toYaw( void ) { 42 float yaw; 43 44 if ( ( y == 0 ) && ( x == 0 ) ) { 45 yaw = 0; 46 } else { 47 yaw = atan2( y, x ) * 180 / M_PI; 48 if ( yaw < 0 ) { 49 yaw += 360; 50 } 51 } 52 53 return yaw; 54 } 55 56 float idVec3_t::toPitch( void ) { 57 float forward; 58 float pitch; 59 60 if ( ( x == 0 ) && ( y == 0 ) ) { 61 if ( z > 0 ) { 62 pitch = 90; 63 } else { 64 pitch = 270; 65 } 66 } else { 67 forward = ( float )idSqrt( x * x + y * y ); 68 pitch = atan2( z, forward ) * 180 / M_PI; 69 if ( pitch < 0 ) { 70 pitch += 360; 71 } 72 } 73 74 return pitch; 75 } 76 77 /* 78 angles_t idVec3_t::toAngles( void ) { 79 float forward; 80 float yaw; 81 float pitch; 82 83 if ( ( x == 0 ) && ( y == 0 ) ) { 84 yaw = 0; 85 if ( z > 0 ) { 86 pitch = 90; 87 } else { 88 pitch = 270; 89 } 90 } else { 91 yaw = atan2( y, x ) * 180 / M_PI; 92 if ( yaw < 0 ) { 93 yaw += 360; 94 } 95 96 forward = ( float )idSqrt( x * x + y * y ); 97 pitch = atan2( z, forward ) * 180 / M_PI; 98 if ( pitch < 0 ) { 99 pitch += 360; 100 } 101 } 102 103 return angles_t( -pitch, yaw, 0 ); 104 } 105 */ 106 107 idVec3_t LerpVector( idVec3_t &w1, idVec3_t &w2, const float t ) { 108 float omega, cosom, sinom, scale0, scale1; 109 110 cosom = w1 * w2; 111 if ( ( 1.0 - cosom ) > LERP_DELTA ) { 112 omega = acos( cosom ); 113 sinom = sin( omega ); 114 scale0 = sin( ( 1.0 - t ) * omega ) / sinom; 115 scale1 = sin( t * omega ) / sinom; 116 } else { 117 scale0 = 1.0 - t; 118 scale1 = t; 119 } 120 121 return ( w1 * scale0 + w2 * scale1 ); 122 } 123 124 /* 125 ============= 126 idVec3_t::string 127 128 This is just a convenience function 129 for printing vectors 130 ============= 131 */ 132 char *idVec3_t::string( void ) { 133 static int index = 0; 134 static char str[ 8 ][ 36 ]; 135 char *s; 136 137 // use an array so that multiple toString's won't collide 138 s = str[ index ]; 139 index = (index + 1)&7; 140 141 sprintf( s, "%.2f %.2f %.2f", x, y, z ); 142 143 return s; 144 }