00001 00002 // 00003 // Das Tank - Copyright (C) 2006, 2007 Windsor Schmidt <windsor@windsorworld.net> 00004 // 00005 // This program is free software; you can redistribute it and/or modify it 00006 // under the terms of the GNU General Public License as published by the Free 00007 // Software Foundation; either version 2 of the License, or (at your option) 00008 // any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, but WITHOUT 00011 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00013 // more details. 00014 // 00015 // You should have received a copy of the GNU General Public License along with 00016 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00017 // Place, Suite 330, Boston, MA 02111-1307 USA 00018 // 00020 00021 00022 const float NORMALIZE_TOLERANCE = 0.0001f; 00023 00025 class Vector 00026 { 00027 public: 00028 float x; 00029 float y; 00030 float z; 00031 00032 Vector(void); 00033 Vector(float iX, float iY, float iZ); 00034 00035 float Magnitude(void); 00036 void Normalize(void); 00037 void Reverse(void); 00038 00039 Vector& operator+=(Vector u); 00040 Vector& operator-=(Vector u); 00041 Vector& operator*=(float s); 00042 Vector& operator/=(float s); 00043 00044 Vector operator-(void); 00045 }; 00046 00048 inline Vector::Vector(void) 00049 { 00050 x = 0; 00051 y = 0; 00052 z = 0; 00053 } 00054 00056 inline Vector::Vector(float iX, float iY, float iZ) 00057 { 00058 x = iX; 00059 y = iY; 00060 z = iZ; 00061 } 00062 00064 inline float Vector::Magnitude(void) 00065 { 00066 return (float) sqrt(x*x + y*y + z*z); 00067 } 00068 00070 inline void Vector::Normalize(void) 00071 { 00072 float m = (float) sqrt(x*x + y*y + z*z); 00073 if(m <= NORMALIZE_TOLERANCE) m = 1; 00074 00075 x /= m; 00076 y /= m; 00077 z /= m; 00078 00079 if (fabs(x) < NORMALIZE_TOLERANCE) x = 0.0f; 00080 if (fabs(y) < NORMALIZE_TOLERANCE) y = 0.0f; 00081 if (fabs(z) < NORMALIZE_TOLERANCE) z = 0.0f; 00082 } 00083 00085 inline void Vector::Reverse(void) 00086 { 00087 x = -x; 00088 y = -y; 00089 z = -z; 00090 } 00091 00093 inline Vector& Vector::operator+=(Vector u) 00094 { 00095 x += u.x; 00096 y += u.y; 00097 z += u.z; 00098 return *this; 00099 } 00100 00102 inline Vector& Vector::operator-=(Vector u) 00103 { 00104 x -= u.x; 00105 y -= u.y; 00106 z -= u.z; 00107 return *this; 00108 } 00109 00111 inline Vector& Vector::operator*=(float s) 00112 { 00113 x *= s; 00114 y *= s; 00115 z *= s; 00116 return *this; 00117 } 00118 00120 inline Vector& Vector::operator/=(float s) 00121 { 00122 x /= s; 00123 y /= s; 00124 z /= s; 00125 return *this; 00126 } 00127 00129 inline Vector Vector::operator-(void) 00130 { 00131 return Vector(-x, -y, -z); 00132 } 00133 00135 inline Vector operator+(Vector u, Vector v) 00136 { 00137 return Vector(u.x + v.x, u.y + v.y, u.z + v.z); 00138 } 00139 00141 inline Vector operator-(Vector u, Vector v) 00142 { 00143 return Vector(u.x - v.x, u.y - v.y, u.z - v.z); 00144 } 00145 00147 inline Vector operator^(Vector u, Vector v) 00148 { 00149 return Vector(u.y*v.z - u.z*v.y, -u.x*v.z + u.z*v.x, u.x*v.y - u.y*v.x); 00150 } 00151 00153 inline float operator*(Vector u, Vector v) 00154 { 00155 return (u.x*v.x + u.y*v.y + u.z*v.z); 00156 } 00157 00159 inline Vector operator*(float s, Vector u) 00160 { 00161 return Vector(u.x*s, u.y*s, u.z*s); 00162 } 00163 00165 inline Vector operator*(Vector u, float s) 00166 { 00167 return Vector(u.x*s, u.y*s, u.z*s); 00168 } 00169 00171 inline Vector operator/(Vector u, float s) 00172 { 00173 return Vector(u.x/s, u.y/s, u.z/s); 00174 } 00175 00177 inline float TripleScalarProduct(Vector u, Vector v, Vector w) 00178 { 00179 return (float(u.x*(v.y*w.z-v.z*w.y))+u.y*(-v.x*w.z+v.z*w.x))+(u.z*(v.x*w.y-v.y*w.x)); 00180 } 00181 00183 inline Vector VRotate2D( float angle, Vector u) 00184 { 00185 float x,y; 00186 x = u.x * cos(-angle) + u.y * sin(-angle); 00187 y = -u.x * sin(-angle) + u.y * cos(-angle); 00188 return Vector( x, y, 0); 00189 }
Copyright Windsor Schmidt 2006 - All rights reserved.