00001 #ifndef VEC3_H_INCLUDED
00002 #define VEC3_H_INCLUDED
00003
00004 #include <iostream>
00005 #include <sstream>
00006 #include <string>
00007 #include <math.h>
00008 #include <cmath>
00009 #include "utMath.h"
00010
00016 class Vec3 {
00017 public:
00021 float x;
00025 float y;
00029 float z;
00030 public:
00037 Vec3(const float x=0.0f, const float y=0.0f, const float z=0.0f)
00038 : x(x), y(y), z(z) {
00039 }
00044 Vec3(const Vec3f v)
00045 : x(v.x), y(v.y), z(v.z) {
00046 }
00047 ~Vec3() {
00048 }
00049
00051
00053
00059 Vec3 &operator+=(const Vec3 &b) {
00060 x += b.x;
00061 y += b.y;
00062 z += b.z;
00063 return *this;
00064 }
00065
00072 Vec3 operator+(const Vec3 &b) const {
00073 return Vec3(x + b.x, y + b.y, z + b.z);
00074 }
00075
00082 Vec3 operator-(const Vec3 &b) const {
00083 return Vec3(x - b.x, y - b.y, z - b.z);
00084 }
00085
00092 Vec3 operator*(const float b) const {
00093 return Vec3(x * b, y * b, z * b);
00094 }
00095
00101 Vec3 &operator*=(const float b) {
00102 x *= b;
00103 y *= b;
00104 z *= b;
00105 return *this;
00106 }
00107
00113 Vec3 &operator/=(const float b) {
00114 x /= b;
00115 y /= b;
00116 z /= b;
00117 return *this;
00118 }
00119
00121
00123
00128 void rotateXZ(float ry) {
00129 float cosry = cos(degToRad(ry));
00130 float sinry = sin(degToRad(ry));
00131 float newz = (z * cosry) - (x * sinry);
00132 x = (z * sinry) + (x * cosry);
00133 z = newz;
00134
00135 }
00136
00141 void rotateXY(float rz) {
00142 float cosrz = cos(degToRad(rz));
00143 float sinrz = sin(degToRad(rz));
00144 float newy = (y * cosrz) - (x * sinrz);
00145 x = (y * sinrz) + (x * cosrz);
00146 y = newy;
00147 }
00148
00153 void rotateYZ(float rx) {
00154 float cosrx = cos(degToRad(rx));
00155 float sinrx = sin(degToRad(rx));
00156 float newz = (z * cosrx) + (y * sinrx);
00157 y = -(z * sinrx) + (y * cosrx);
00158
00159
00160
00161
00162
00163
00164 z = newz;
00165 }
00166
00171 float getLengthSquared() const {
00172 return (x * x) + (y * y) + (z * z);
00173 }
00174
00181 float getEuclideanDistance(const Vec3 pos2) const {
00182 float preResultX = (x - pos2.x)*(x - pos2.x);
00183 float preResultY = (y - pos2.y)*(y - pos2.y);
00184 float preResultZ = (z - pos2.z)*(z - pos2.z);
00185 return sqrt(preResultX + preResultY + preResultZ);
00186 }
00187
00193 float getAngle(Vec3 playerPos) const {
00194 float dx = playerPos.x - x;
00195 float dz = playerPos.z - z;
00196 float result = atan2(dx, dz) * 180 / 3.1415f;
00197 return result;
00198 }
00199
00204 Vec3f toVec3f() const {
00205 return Vec3f(x,y,z);
00206 }
00207
00212 std::string toString() {
00213 std::ostringstream result;
00214 result << "[ x: " << x << " y: " << y << " z: " << z << " ]";
00215 return result.str();
00216 }
00217 };
00218 #endif