2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "math_vector.h"
32 #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
34 #define LERP_DELTA 1e-6
36 idVec3 vec_zero( 0.0f, 0.0f, 0.0f );
40 float idVec3::toYaw( void ) {
43 if ( ( y == 0 ) && ( x == 0 ) ) {
47 yaw = atan2( y, x ) * 180 / M_PI;
56 float idVec3::toPitch( void ) {
60 if ( ( x == 0 ) && ( y == 0 ) ) {
69 forward = ( float )idSqrt( x * x + y * y );
70 pitch = atan2( z, forward ) * 180 / M_PI;
80 angles_t idVec3::toAngles( void ) {
85 if ( ( x == 0 ) && ( y == 0 ) ) {
93 yaw = atan2( y, x ) * 180 / M_PI;
98 forward = ( float )idSqrt( x * x + y * y );
99 pitch = atan2( z, forward ) * 180 / M_PI;
105 return angles_t( -pitch, yaw, 0 );
109 idVec3 LerpVector( idVec3 &w1, idVec3 &w2, const float t ) {
110 float omega, cosom, sinom, scale0, scale1;
113 if ( ( 1.0 - cosom ) > LERP_DELTA ) {
114 omega = acos( cosom );
115 sinom = sin( omega );
116 scale0 = sin( ( 1.0 - t ) * omega ) / sinom;
117 scale1 = sin( t * omega ) / sinom;
124 return ( w1 * scale0 + w2 * scale1 );
131 This is just a convenience function
135 char *idVec3::string( void ) {
136 static int index = 0;
137 static char str[ 8 ][ 36 ];
140 // use an array so that multiple toString's won't collide
142 index = ( index + 1 ) & 7;
144 sprintf( s, "%.2f %.2f %.2f", x, y, z );