diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Decals.cpp | 2 | ||||
-rw-r--r-- | src/GameLoop.cpp | 4 | ||||
-rw-r--r-- | src/Models.cpp | 1 | ||||
-rw-r--r-- | src/PhysicsMath.h | 1190 | ||||
-rw-r--r-- | src/Quaternions.cpp | 388 | ||||
-rw-r--r-- | src/Quaternions.h | 48 | ||||
-rw-r--r-- | src/Skeleton.cpp | 3 | ||||
-rw-r--r-- | src/Sprites.cpp | 2 |
8 files changed, 17 insertions, 1621 deletions
diff --git a/src/Decals.cpp b/src/Decals.cpp index 74fe379..92888ec 100644 --- a/src/Decals.cpp +++ b/src/Decals.cpp @@ -1,3 +1,5 @@ +#include <cmath> + #include "Camera.h" #include "Constants.h" #include "Decals.h" diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp index 9aaff92..caa3c1b 100644 --- a/src/GameLoop.cpp +++ b/src/GameLoop.cpp @@ -19,6 +19,8 @@ // You should have received a copy of the GNU General Public License // along with Black Shades. If not, see <https://www.gnu.org/licenses/>. +#include <cmath> + #include "Game.h" #include "misc.h" @@ -142,7 +144,7 @@ void handleKey(Game* game, int key, int action, int mods) XYZ towards = player.playercoords - game->bodycoords; if (towards.x || towards.z) { Normalise(&towards); - camera.rotation = RadiansToDegrees(asin(towards.x)); + camera.rotation = asin(towards.x) * 180.0f / M_PI; if (towards.z > 0) camera.rotation = 180 - camera.rotation; diff --git a/src/Models.cpp b/src/Models.cpp index ff5c0e2..4c92f93 100644 --- a/src/Models.cpp +++ b/src/Models.cpp @@ -1,4 +1,5 @@ #include <algorithm> +#include <cmath> #include "Models.h" #include "misc.h" diff --git a/src/PhysicsMath.h b/src/PhysicsMath.h deleted file mode 100644 index c6c320c..0000000 --- a/src/PhysicsMath.h +++ /dev/null @@ -1,1190 +0,0 @@ -#ifndef _PHYSICSMATH_H_ - -#define _PHYSICSMATH_H_ - -#include <cmath> - -#include "Quaternions.h" - -//------------------------------------------------------------------------// - -// Misc. Constants - -//------------------------------------------------------------------------// - -const float pi = acosf(-1); - -float const g = -32.174f; // acceleration due to gravity, ft/s^2 - -float const rho = 0.0023769f; // desity of air at sea level, slugs/ft^3 - -float const tol = 0.0000000001f; // float type tolerance - -//------------------------------------------------------------------------// - -// Misc. Functions - -//------------------------------------------------------------------------// - -inline float DegreesToRadians(float deg); - -inline float RadiansToDegrees(float rad); - -inline float DegreesToRadians(float deg) -{ - return deg * pi / 180.0f; -} - -inline float RadiansToDegrees(float rad) -{ - return rad * 180.0f / pi; -} - -//------------------------------------------------------------------------// - -// Vector Class and vector functions - -//------------------------------------------------------------------------// - -class Vector { - -public: - - float x; - - float y; - - float z; - - Vector(void); - - Vector(float xi, float yi, float zi); - - float Magnitude(void); - - void Normalize(void); - - void Reverse(void); - - Vector& operator+=(Vector u); // vector addition - - Vector& operator-=(Vector u); // vector subtraction - - Vector& operator*=(float s); // scalar multiply - - Vector& operator/=(float s); // scalar divide - - Vector operator-(void); - -}; - -inline Vector operator+(Vector u, Vector v); - -inline Vector operator-(Vector u, Vector v); - -inline Vector operator^(Vector u, Vector v); - -inline float operator*(Vector u, Vector v); - -inline Vector operator*(float s, Vector u); - -inline Vector operator*(Vector u, float s); - -inline Vector operator/(Vector u, float s); - -inline float TripleScalarProduct(Vector u, Vector v, Vector w); - -inline Vector::Vector(void) - -{ - - x = 0; - - y = 0; - - z = 0; - -} - -inline Vector::Vector(float xi, float yi, float zi) - -{ - - x = xi; - - y = yi; - - z = zi; - -} - -inline float Vector::Magnitude(void) - -{ - - return (float) sqrt(x*x + y*y + z*z); - -} - -inline void Vector::Normalize(void) - -{ - - float m = (float) sqrt(x*x + y*y + z*z); - - if(m <= tol) m = 1; - - x /= m; - - y /= m; - - z /= m; - - if (fabs(x) < tol) x = 0.0f; - - if (fabs(y) < tol) y = 0.0f; - - if (fabs(z) < tol) z = 0.0f; - -} - -inline void Vector::Reverse(void) - -{ - - x = -x; - - y = -y; - - z = -z; - -} - -inline Vector& Vector::operator+=(Vector u) - -{ - - x += u.x; - - y += u.y; - - z += u.z; - - return *this; - -} - -inline Vector& Vector::operator-=(Vector u) - -{ - - x -= u.x; - - y -= u.y; - - z -= u.z; - - return *this; - -} - -inline Vector& Vector::operator*=(float s) - -{ - - x *= s; - - y *= s; - - z *= s; - - return *this; - -} - -inline Vector& Vector::operator/=(float s) - -{ - - x /= s; - - y /= s; - - z /= s; - - return *this; - -} - -inline Vector Vector::operator-(void) - -{ - - return Vector(-x, -y, -z); - -} - -inline Vector operator+(Vector u, Vector v) - -{ - - return Vector(u.x + v.x, u.y + v.y, u.z + v.z); - -} - -inline Vector operator-(Vector u, Vector v) - -{ - - return Vector(u.x - v.x, u.y - v.y, u.z - v.z); - -} - -// Vector cross product (u cross v) - -inline Vector operator^(Vector u, Vector v) - -{ - - 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 ); - -} - -// Vector dot product - -inline float operator*(Vector u, Vector v) - -{ - - return (u.x*v.x + u.y*v.y + u.z*v.z); - -} - -inline Vector operator*(float s, Vector u) - -{ - - return Vector(u.x*s, u.y*s, u.z*s); - -} - -inline Vector operator*(Vector u, float s) - -{ - - return Vector(u.x*s, u.y*s, u.z*s); - -} - -inline Vector operator/(Vector u, float s) - -{ - - return Vector(u.x/s, u.y/s, u.z/s); - -} - -// triple scalar product (u dot (v cross w)) - -inline float TripleScalarProduct(Vector u, Vector v, Vector w) - -{ - - 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)) ); - - //return u*(v^w); - -} - -//------------------------------------------------------------------------// - -// Matrix Class and matrix functions - -//------------------------------------------------------------------------// - -class Matrix3x3 { - -public: - - // elements eij: i -> row, j -> column - - float e11, e12, e13, e21, e22, e23, e31, e32, e33; - - Matrix3x3(void); - - Matrix3x3( float r1c1, float r1c2, float r1c3, - - float r2c1, float r2c2, float r2c3, - - float r3c1, float r3c2, float r3c3 ); - - float det(void); - - Matrix3x3 Transpose(void); - - Matrix3x3 Inverse(void); - - Matrix3x3& operator+=(Matrix3x3 m); - - Matrix3x3& operator-=(Matrix3x3 m); - - Matrix3x3& operator*=(float s); - - Matrix3x3& operator/=(float s); - -}; - -inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2); - -inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2); - -inline Matrix3x3 operator/(Matrix3x3 m, float s); - -inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2); - -inline Matrix3x3 operator*(Matrix3x3 m, float s); - -inline Matrix3x3 operator*(float s, Matrix3x3 m); - -inline Vector operator*(Matrix3x3 m, Vector u); - -inline Vector operator*(Vector u, Matrix3x3 m); - -inline Matrix3x3::Matrix3x3(void) - -{ - - e11 = 0; - - e12 = 0; - - e13 = 0; - - e21 = 0; - - e22 = 0; - - e23 = 0; - - e31 = 0; - - e32 = 0; - - e33 = 0; - -} - -inline Matrix3x3::Matrix3x3( float r1c1, float r1c2, float r1c3, - - float r2c1, float r2c2, float r2c3, - - float r3c1, float r3c2, float r3c3 ) - -{ - - e11 = r1c1; - - e12 = r1c2; - - e13 = r1c3; - - e21 = r2c1; - - e22 = r2c2; - - e23 = r2c3; - - e31 = r3c1; - - e32 = r3c2; - - e33 = r3c3; - -} - -inline float Matrix3x3::det(void) - -{ - - return e11*e22*e33 - - - e11*e32*e23 + - - e21*e32*e13 - - - e21*e12*e33 + - - e31*e12*e23 - - - e31*e22*e13; - -} - -inline Matrix3x3 Matrix3x3::Transpose(void) - -{ - - return Matrix3x3(e11,e21,e31,e12,e22,e32,e13,e23,e33); - -} - -inline Matrix3x3 Matrix3x3::Inverse(void) - -{ - - float d = e11*e22*e33 - - - e11*e32*e23 + - - e21*e32*e13 - - - e21*e12*e33 + - - e31*e12*e23 - - - e31*e22*e13; - - if (abs(d) < 0.01f) - d = 1; - - return Matrix3x3( (e22*e33-e23*e32)/d, - - -(e12*e33-e13*e32)/d, - - (e12*e23-e13*e22)/d, - - -(e21*e33-e23*e31)/d, - - (e11*e33-e13*e31)/d, - - -(e11*e23-e13*e21)/d, - - (e21*e32-e22*e31)/d, - - -(e11*e32-e12*e31)/d, - - (e11*e22-e12*e21)/d ); - -} - -inline Matrix3x3& Matrix3x3::operator+=(Matrix3x3 m) - -{ - - e11 += m.e11; - - e12 += m.e12; - - e13 += m.e13; - - e21 += m.e21; - - e22 += m.e22; - - e23 += m.e23; - - e31 += m.e31; - - e32 += m.e32; - - e33 += m.e33; - - return *this; - -} - -inline Matrix3x3& Matrix3x3::operator-=(Matrix3x3 m) - -{ - - e11 -= m.e11; - - e12 -= m.e12; - - e13 -= m.e13; - - e21 -= m.e21; - - e22 -= m.e22; - - e23 -= m.e23; - - e31 -= m.e31; - - e32 -= m.e32; - - e33 -= m.e33; - - return *this; - -} - -inline Matrix3x3& Matrix3x3::operator*=(float s) - -{ - - e11 *= s; - - e12 *= s; - - e13 *= s; - - e21 *= s; - - e22 *= s; - - e23 *= s; - - e31 *= s; - - e32 *= s; - - e33 *= s; - - return *this; - -} - -inline Matrix3x3& Matrix3x3::operator/=(float s) - -{ - - e11 /= s; - - e12 /= s; - - e13 /= s; - - e21 /= s; - - e22 /= s; - - e23 /= s; - - e31 /= s; - - e32 /= s; - - e33 /= s; - - return *this; - -} - -inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2) - -{ - - return Matrix3x3( m1.e11+m2.e11, - - m1.e12+m2.e12, - - m1.e13+m2.e13, - - m1.e21+m2.e21, - - m1.e22+m2.e22, - - m1.e23+m2.e23, - - m1.e31+m2.e31, - - m1.e32+m2.e32, - - m1.e33+m2.e33); - -} - -inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2) - -{ - - return Matrix3x3( m1.e11-m2.e11, - - m1.e12-m2.e12, - - m1.e13-m2.e13, - - m1.e21-m2.e21, - - m1.e22-m2.e22, - - m1.e23-m2.e23, - - m1.e31-m2.e31, - - m1.e32-m2.e32, - - m1.e33-m2.e33); - -} - -inline Matrix3x3 operator/(Matrix3x3 m, float s) - -{ - - return Matrix3x3( m.e11/s, - - m.e12/s, - - m.e13/s, - - m.e21/s, - - m.e22/s, - - m.e23/s, - - m.e31/s, - - m.e32/s, - - m.e33/s); - -} - -inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2) - -{ - - return Matrix3x3( m1.e11*m2.e11 + m1.e12*m2.e21 + m1.e13*m2.e31, - - m1.e11*m2.e12 + m1.e12*m2.e22 + m1.e13*m2.e32, - - m1.e11*m2.e13 + m1.e12*m2.e23 + m1.e13*m2.e33, - - m1.e21*m2.e11 + m1.e22*m2.e21 + m1.e23*m2.e31, - - m1.e21*m2.e12 + m1.e22*m2.e22 + m1.e23*m2.e32, - - m1.e21*m2.e13 + m1.e22*m2.e23 + m1.e23*m2.e33, - - m1.e31*m2.e11 + m1.e32*m2.e21 + m1.e33*m2.e31, - - m1.e31*m2.e12 + m1.e32*m2.e22 + m1.e33*m2.e32, - - m1.e31*m2.e13 + m1.e32*m2.e23 + m1.e33*m2.e33 ); - -} - -inline Matrix3x3 operator*(Matrix3x3 m, float s) - -{ - - return Matrix3x3( m.e11*s, - - m.e12*s, - - m.e13*s, - - m.e21*s, - - m.e22*s, - - m.e23*s, - - m.e31*s, - - m.e32*s, - - m.e33*s); - -} - -inline Matrix3x3 operator*(float s, Matrix3x3 m) - -{ - - return Matrix3x3( m.e11*s, - - m.e12*s, - - m.e13*s, - - m.e21*s, - - m.e22*s, - - m.e23*s, - - m.e31*s, - - m.e32*s, - - m.e33*s); - -} - -inline Vector operator*(Matrix3x3 m, Vector u) - -{ - - return Vector( m.e11*u.x + m.e12*u.y + m.e13*u.z, - - m.e21*u.x + m.e22*u.y + m.e23*u.z, - - m.e31*u.x + m.e32*u.y + m.e33*u.z); - -} - -inline Vector operator*(Vector u, Matrix3x3 m) - -{ - - return Vector( u.x*m.e11 + u.y*m.e21 + u.z*m.e31, - - u.x*m.e12 + u.y*m.e22 + u.z*m.e32, - - u.x*m.e13 + u.y*m.e23 + u.z*m.e33); - -} - -//------------------------------------------------------------------------// - -// Quaternion Class and Quaternion functions - -//------------------------------------------------------------------------// - -class Quaternion { - -public: - - float n; // number (scalar) part - - Vector v; // vector part: v.x, v.y, v.z - - Quaternion(void); - - Quaternion(float e0, float e1, float e2, float e3); - - float Magnitude(void); - - Vector GetVector(void); - - float GetScalar(void); - - Quaternion operator+=(Quaternion q); - - Quaternion operator-=(Quaternion q); - - Quaternion operator*=(float s); - - Quaternion operator/=(float s); - - Quaternion operator~(void) const { return Quaternion(n, -v.x, -v.y, -v.z);} - -}; - -inline Quaternion operator+(Quaternion q1, Quaternion q2); - -inline Quaternion operator-(Quaternion q1, Quaternion q2); - -inline Quaternion operator*(Quaternion q1, Quaternion q2); - -inline Quaternion operator*(Quaternion q, float s); - -inline Quaternion operator*(float s, Quaternion q); - -inline Quaternion operator*(Quaternion q, Vector v); - -inline Quaternion operator*(Vector v, Quaternion q); - -inline Quaternion operator/(Quaternion q, float s); - -inline float QGetAngle(Quaternion q); - -inline Vector QGetAxis(Quaternion q); - -inline Quaternion QRotate(Quaternion q1, Quaternion q2); - -inline Vector QVRotate(Quaternion q, Vector v); - -inline Quaternion MakeQFromEulerAngles(float x, float y, float z); - -inline Vector MakeEulerAnglesFromQ(Quaternion q); - -inline Quaternion::Quaternion(void) - -{ - - n = 0; - - v.x = 0; - - v.y = 0; - - v.z = 0; - -} - -inline Quaternion::Quaternion(float e0, float e1, float e2, float e3) - -{ - - n = e0; - - v.x = e1; - - v.y = e2; - - v.z = e3; - -} - -inline float Quaternion::Magnitude(void) - -{ - - return (float) sqrt(n*n + v.x*v.x + v.y*v.y + v.z*v.z); - -} - -inline Vector Quaternion::GetVector(void) - -{ - - return Vector(v.x, v.y, v.z); - -} - -inline float Quaternion::GetScalar(void) - -{ - - return n; - -} - -inline Quaternion Quaternion::operator+=(Quaternion q) - -{ - - n += q.n; - - v.x += q.v.x; - - v.y += q.v.y; - - v.z += q.v.z; - - return *this; - -} - -inline Quaternion Quaternion::operator-=(Quaternion q) - -{ - - n -= q.n; - - v.x -= q.v.x; - - v.y -= q.v.y; - - v.z -= q.v.z; - - return *this; - -} - -inline Quaternion Quaternion::operator*=(float s) - -{ - - n *= s; - - v.x *= s; - - v.y *= s; - - v.z *= s; - - return *this; - -} - -inline Quaternion Quaternion::operator/=(float s) - -{ - - n /= s; - - v.x /= s; - - v.y /= s; - - v.z /= s; - - return *this; - -} - -/*inline Quaternion Quaternion::operator~() - -{ - - return Quaternion(n, -v.x, -v.y, -v.z); - -}*/ - -inline Quaternion operator+(Quaternion q1, Quaternion q2) - -{ - - return Quaternion( q1.n + q2.n, - - q1.v.x + q2.v.x, - - q1.v.y + q2.v.y, - - q1.v.z + q2.v.z); - -} - -inline Quaternion operator-(Quaternion q1, Quaternion q2) - -{ - - return Quaternion( q1.n - q2.n, - - q1.v.x - q2.v.x, - - q1.v.y - q2.v.y, - - q1.v.z - q2.v.z); - -} - -inline Quaternion operator*(Quaternion q1, Quaternion q2) - -{ - - return Quaternion( q1.n*q2.n - q1.v.x*q2.v.x - q1.v.y*q2.v.y - q1.v.z*q2.v.z, - - q1.n*q2.v.x + q1.v.x*q2.n + q1.v.y*q2.v.z - q1.v.z*q2.v.y, - - q1.n*q2.v.y + q1.v.y*q2.n + q1.v.z*q2.v.x - q1.v.x*q2.v.z, - - q1.n*q2.v.z + q1.v.z*q2.n + q1.v.x*q2.v.y - q1.v.y*q2.v.x); - -} - -inline Quaternion operator*(Quaternion q, float s) - -{ - - return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s); - -} - -inline Quaternion operator*(float s, Quaternion q) - -{ - - return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s); - -} - -inline Quaternion operator*(Quaternion q, Vector v) - -{ - - return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z), - - q.n*v.x + q.v.y*v.z - q.v.z*v.y, - - q.n*v.y + q.v.z*v.x - q.v.x*v.z, - - q.n*v.z + q.v.x*v.y - q.v.y*v.x); - -} - -inline Quaternion operator*(Vector v, Quaternion q) - -{ - - return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z), - - q.n*v.x + q.v.z*v.y - q.v.y*v.z, - - q.n*v.y + q.v.x*v.z - q.v.z*v.x, - - q.n*v.z + q.v.y*v.x - q.v.x*v.y); - -} - -inline Quaternion operator/(Quaternion q, float s) - -{ - - return Quaternion(q.n/s, q.v.x/s, q.v.y/s, q.v.z/s); - -} - -inline float QGetAngle(Quaternion q) - -{ - - return (float) (2*acos(q.n)); - -} - -inline Vector QGetAxis(Quaternion q) - -{ - - Vector v; - - float m; - - v = q.GetVector(); - - m = v.Magnitude(); - - if (m <= tol) - - return Vector(); - - else - - return v/m; - -} - -inline Quaternion QRotate(Quaternion q1, Quaternion q2) - -{ - - return q1*q2*(~q1); - -} - -inline Vector QVRotate(Quaternion q, Vector v) - -{ - - Quaternion t; - - t = q*v*(~q); - - return t.GetVector(); - -} - -inline Quaternion MakeQFromEulerAngles(float x, float y, float z) - -{ - - Quaternion q; - - double roll = DegreesToRadians(x); - - double pitch = DegreesToRadians(y); - - double yaw = DegreesToRadians(z); - - double cyaw, cpitch, croll, syaw, spitch, sroll; - - double cyawcpitch, syawspitch, cyawspitch, syawcpitch; - - cyaw = cos(0.5f * yaw); - - cpitch = cos(0.5f * pitch); - - croll = cos(0.5f * roll); - - syaw = sin(0.5f * yaw); - - spitch = sin(0.5f * pitch); - - sroll = sin(0.5f * roll); - - cyawcpitch = cyaw*cpitch; - - syawspitch = syaw*spitch; - - cyawspitch = cyaw*spitch; - - syawcpitch = syaw*cpitch; - - q.n = (float) (cyawcpitch * croll + syawspitch * sroll); - - q.v.x = (float) (cyawcpitch * sroll - syawspitch * croll); - - q.v.y = (float) (cyawspitch * croll + syawcpitch * sroll); - - q.v.z = (float) (syawcpitch * croll - cyawspitch * sroll); - - return q; - -} - -inline Vector MakeEulerAnglesFromQ(Quaternion q) - -{ - - double r11, r21, r31, r32, r33, r12, r13; - - double q00, q11, q22, q33; - - double tmp; - - Vector u; - - q00 = q.n * q.n; - - q11 = q.v.x * q.v.x; - - q22 = q.v.y * q.v.y; - - q33 = q.v.z * q.v.z; - - r11 = q00 + q11 - q22 - q33; - - r21 = 2 * (q.v.x*q.v.y + q.n*q.v.z); - - r31 = 2 * (q.v.x*q.v.z - q.n*q.v.y); - - r32 = 2 * (q.v.y*q.v.z + q.n*q.v.x); - - r33 = q00 - q11 - q22 + q33; - - tmp = fabs(r31); - - if(tmp > 0.999999) - - { - - r12 = 2 * (q.v.x*q.v.y - q.n*q.v.z); - - r13 = 2 * (q.v.x*q.v.z + q.n*q.v.y); - - u.x = RadiansToDegrees(0.0f); //roll - - u.y = RadiansToDegrees((float) (-(pi/2) * r31/tmp)); // pitch - - u.z = RadiansToDegrees((float) atan2(-r12, -r31*r13)); // yaw - - return u; - - } - - u.x = RadiansToDegrees((float) atan2(r32, r33)); // roll - - u.y = RadiansToDegrees((float) asin(-r31)); // pitch - - u.z = RadiansToDegrees((float) atan2(r21, r11)); // yaw - - return u; - -} - -#endif diff --git a/src/Quaternions.cpp b/src/Quaternions.cpp index 90c7502..fc38c44 100644 --- a/src/Quaternions.cpp +++ b/src/Quaternions.cpp @@ -1,24 +1,6 @@ -#include "Quaternions.h" +#include <cmath> -// Functions -quaternion Quat_Mult(quaternion q1, quaternion q2) -{ - quaternion QResult; - float a, b, c, d, e, f, g, h; - a = (q1.w + q1.x) * (q2.w + q2.x); - b = (q1.z - q1.y) * (q2.y - q2.z); - c = (q1.w - q1.x) * (q2.y + q2.z); - d = (q1.y + q1.z) * (q2.w - q2.x); - e = (q1.x + q1.z) * (q2.x + q2.y); - f = (q1.x - q1.z) * (q2.x - q2.y); - g = (q1.w + q1.y) * (q2.w - q2.z); - h = (q1.w - q1.y) * (q2.w + q2.z); - QResult.w = b + (-e - f + g + h) / 2; - QResult.x = a - (e + f + g + h) / 2; - QResult.y = c + (e - f + g - h) / 2; - QResult.z = d + (e - f - g + h) / 2; - return QResult; -} +#include "Quaternions.h" XYZ XYZ::operator+(XYZ add){ XYZ ne; @@ -98,177 +80,11 @@ void XYZ::operator=(float add){ z=add; } -void XYZ::vec(Vector add){ - x=add.x; - y=add.y; - z=add.z; -} - bool XYZ::operator==(XYZ add){ if(x==add.x&&y==add.y&&z==add.z)return 1; return 0; } -quaternion To_Quat(Matrix_t m) -{ - // From Jason Shankel, (C) 2000. - quaternion Quat; - - double Tr = m[0][0] + m[1][1] + m[2][2] + 1.0, fourD; - double q[4]; - - int i,j,k; - if (Tr >= 1.0) - { - fourD = 2.0 * sqrt(Tr); - q[3] = fourD/4.0; - q[0] = (m[2][1] - m[1][2]) / fourD; - q[1] = (m[0][2] - m[2][0]) / fourD; - q[2] = (m[1][0] - m[0][1]) / fourD; - } - else - { - if (m[0][0] > m[1][1]) - { - i = 0; - } - else - { - i = 1; - } - if (m[2][2] > m[i][i]) - { - i = 2; - } - j = (i+1)%3; - k = (j+1)%3; - fourD = 2.0 * sqrt(m[i][i] - m[j][j] - m[k][k] + 1.0); - q[i] = fourD / 4.0; - q[j] = (m[j][i] + m[i][j]) / fourD; - q[k] = (m[k][i] + m[i][k]) / fourD; - q[3] = (m[j][k] - m[k][j]) / fourD; - } - - Quat.x = q[0]; - Quat.y = q[1]; - Quat.z = q[2]; - Quat.w = q[3]; - return Quat; -} -void Quat_2_Matrix(quaternion Quat, Matrix_t m) -{ - // From the GLVelocity site (http://glvelocity.gamedev.net) - float fW = Quat.w; - float fX = Quat.x; - float fY = Quat.y; - float fZ = Quat.z; - float fXX = fX * fX; - float fYY = fY * fY; - float fZZ = fZ * fZ; - m[0][0] = 1.0f - 2.0f * (fYY + fZZ); - m[1][0] = 2.0f * (fX * fY + fW * fZ); - m[2][0] = 2.0f * (fX * fZ - fW * fY); - m[3][0] = 0.0f; - m[0][1] = 2.0f * (fX * fY - fW * fZ); - m[1][1] = 1.0f - 2.0f * (fXX + fZZ); - m[2][1] = 2.0f * (fY * fZ + fW * fX); - m[3][1] = 0.0f; - m[0][2] = 2.0f * (fX * fZ + fW * fY); - m[1][2] = 2.0f * (fX * fZ - fW * fX); - m[2][2] = 1.0f - 2.0f * (fXX + fYY); - m[3][2] = 0.0f; - m[0][3] = 0.0f; - m[1][3] = 0.0f; - m[2][3] = 0.0f; - m[3][3] = 1.0f; -} -quaternion To_Quat(angle_axis Ang_Ax) -{ - // From the Quaternion Powers article on gamedev.net - quaternion Quat; - - Quat.x = Ang_Ax.x * sin(Ang_Ax.angle / 2); - Quat.y = Ang_Ax.y * sin(Ang_Ax.angle / 2); - Quat.z = Ang_Ax.z * sin(Ang_Ax.angle / 2); - Quat.w = cos(Ang_Ax.angle / 2); - return Quat; -} -angle_axis Quat_2_AA(quaternion Quat) -{ - angle_axis Ang_Ax; - float scale, tw; - tw = (float)acos(Quat.w) * 2; - scale = (float)sin(tw / 2.0); - Ang_Ax.x = Quat.x / scale; - Ang_Ax.y = Quat.y / scale; - Ang_Ax.z = Quat.z / scale; - - Ang_Ax.angle = 2.0 * acos(Quat.w)/(float)PI*180; - return Ang_Ax; -} - -quaternion To_Quat(int In_Degrees, euler Euler) -{ - // From the gamasutra quaternion article - quaternion Quat; - float cr, cp, cy, sr, sp, sy, cpcy, spsy; - //If we are in Degree mode, convert to Radians - if (In_Degrees) { - Euler.x = Euler.x * (float)PI / 180; - Euler.y = Euler.y * (float)PI / 180; - Euler.z = Euler.z * (float)PI / 180; - } - //Calculate trig identities - //Formerly roll, pitch, yaw - cr = float(cos(Euler.x/2)); - cp = float(cos(Euler.y/2)); - cy = float(cos(Euler.z/2)); - sr = float(sin(Euler.x/2)); - sp = float(sin(Euler.y/2)); - sy = float(sin(Euler.z/2)); - - cpcy = cp * cy; - spsy = sp * sy; - Quat.w = cr * cpcy + sr * spsy; - Quat.x = sr * cpcy - cr * spsy; - Quat.y = cr * sp * cy + sr * cp * sy; - Quat.z = cr * cp * sy - sr * sp * cy; - - return Quat; -} - -quaternion QNormalize(quaternion Quat) -{ - float norm; - norm = Quat.x * Quat.x + - Quat.y * Quat.y + - Quat.z * Quat.z + - Quat.w * Quat.w; - Quat.x = float(Quat.x / norm); - Quat.y = float(Quat.y / norm); - Quat.z = float(Quat.z / norm); - Quat.w = float(Quat.w / norm); - return Quat; -} - -XYZ Quat2Vector(quaternion Quat) -{ - QNormalize(Quat); - - float fW = Quat.w; - float fX = Quat.x; - float fY = Quat.y; - float fZ = Quat.z; - - XYZ tempvec; - - tempvec.x = 2.0f*(fX*fZ-fW*fY); - tempvec.y = 2.0f*(fY*fZ+fW*fX); - tempvec.z = 1.0f-2.0f*(fX*fX+fY*fY); - - return tempvec; -} - void CrossProduct(XYZ P, XYZ Q, XYZ *V){ V->x = P.y * Q.z - P.z * Q.y; V->y = P.z * Q.x - P.x * Q.z; @@ -283,14 +99,6 @@ void Normalise(XYZ *vectory) { vectory->z /= d; } -float normaldotproduct(XYZ point1, XYZ point2){ - GLfloat returnvalue; - Normalise(&point1); - Normalise(&point2); - returnvalue=(point1.x*point2.x+point1.y*point2.y+point1.z*point2.z); - return returnvalue; -} - extern float u0, u1, u2; extern float v0, v1, v2; extern float a, b; @@ -302,99 +110,6 @@ extern float p2v[3]; extern float p3v[3]; extern float normalv[3]; -bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33) -{ - bInter=0; - - pointv[0]=p->x; - pointv[1]=p->y; - pointv[2]=p->z; - - p1v[0]=p11; - p1v[1]=p12; - p1v[2]=p13; - - p2v[0]=p21; - p2v[1]=p22; - p2v[2]=p23; - - p3v[0]=p31; - p3v[1]=p32; - p3v[2]=p33; - - normalv[0]=normal.x; - normalv[1]=normal.y; - normalv[2]=normal.z; - -#define ABS(X) (((X)<0.f)?-(X):(X) ) -#define MAX(A, B) (((A)<(B))?(B):(A)) - float max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2])); -#undef MAX - if (max == ABS(normalv[0])) {i = 1; j = 2;} // y, z - if (max == ABS(normalv[1])) {i = 0; j = 2;} // x, z - if (max == ABS(normalv[2])) {i = 0; j = 1;} // x, y -#undef ABS - - u0 = pointv[i] - p1v[i]; - v0 = pointv[j] - p1v[j]; - u1 = p2v[i] - p1v[i]; - v1 = p2v[j] - p1v[j]; - u2 = p3v[i] - p1v[i]; - v2 = p3v[j] - p1v[j]; - - if (u1 > -1.0e-05f && u1 < 1.0e-05f)// == 0.0f) - { - b = u0 / u2; - if (0.0f <= b && b <= 1.0f) - { - a = (v0 - b * v2) / v1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f)) - bInter = 1; - } - } - else - { - b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1); - if (0.0f <= b && b <= 1.0f) - { - a = (u0 - b * u2) / u1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f )) - bInter = 1; - } - } - - return bInter; -} - -bool LineFacet(Vector p1,Vector p2,Vector pa,Vector pb,Vector pc,Vector *p) -{ - float d; - float denom, mu; - Vector n, pa1, pa2, pa3; - - //Calculate the parameters for the plane - n.x = (pb.y - pa.y)*(pc.z - pa.z) - (pb.z - pa.z)*(pc.y - pa.y); - n.y = (pb.z - pa.z)*(pc.x - pa.x) - (pb.x - pa.x)*(pc.z - pa.z); - n.z = (pb.x - pa.x)*(pc.y - pa.y) - (pb.y - pa.y)*(pc.x - pa.x); - n.Normalize(); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (abs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if(!PointInTriangle( p, n, pa.x, pa.y, pa.z, pb.x, pb.y, pb.z, pc.x, pc.y, pc.z)){return 0;} - - return 1; -} - bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3) { bInter=0; @@ -459,65 +174,11 @@ bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3) return bInter; } -bool LineFacet(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p) -{ - float d; - float denom, mu; - XYZ n; - - //Calculate the parameters for the plane - n.x = (pb.y - pa.y)*(pc.z - pa.z) - (pb.z - pa.z)*(pc.y - pa.y); - n.y = (pb.z - pa.z)*(pc.x - pa.x) - (pb.x - pa.x)*(pc.z - pa.z); - n.z = (pb.x - pa.x)*(pc.y - pa.y) - (pb.y - pa.y)*(pc.x - pa.x); - Normalise(&n); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (abs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;} - - return 1; -} - extern float d; extern float a1,a2,a3; extern float total,denom,mu; extern XYZ pa1,pa2,pa3,n; -float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p) -{ - //Calculate the parameters for the plane - n.x = (pb.y - pa.y)*(pc.z - pa.z) - (pb.z - pa.z)*(pc.y - pa.y); - n.y = (pb.z - pa.z)*(pc.x - pa.x) - (pb.x - pa.x)*(pc.z - pa.z); - n.z = (pb.x - pa.x)*(pc.y - pa.y) - (pb.y - pa.y)*(pc.x - pa.x); - Normalise(&n); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (abs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;} - - return 1; -} - float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc, XYZ n, XYZ *p) { @@ -581,19 +242,13 @@ void ReflectVector(XYZ *vel, XYZ *n) } float dotproduct(XYZ point1, XYZ point2){ - GLfloat returnvalue; - returnvalue=(point1.x*point2.x+point1.y*point2.y+point1.z*point2.z); - return returnvalue; + return point1.x * point2.x + point1.y * point2.y + point1.z * point2.z; } float findDistance(XYZ point1, XYZ point2){ return sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)+(point1.z-point2.z)*(point1.z-point2.z)); } -float findLength(XYZ point1){ - return sqrt((point1.x)*(point1.x)+(point1.y)*(point1.y)+(point1.z)*(point1.z)); -} - float findLengthfast(XYZ point1){ return((point1.x)*(point1.x)+(point1.y)*(point1.y)+(point1.z)*(point1.z)); } @@ -643,10 +298,8 @@ XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang){ float square( float f ) { return (f*f) ;} -bool sphere_line_intersection ( - float x1, float y1 , float z1, - float x2, float y2 , float z2, - float x3, float y3 , float z3, float r ) +bool sphere_line_intersection(float x1, float y1, float z1, + float x2, float y2, float z2, float x3, float y3, float z3, float r) { // x1,y1,z1 P1 coordinates (point of line) @@ -683,34 +336,3 @@ bool sphere_line_intersection ( return(1); } - -XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang){ - XYZ newpoint; - XYZ oldpoint; - - oldpoint=thePoint; - - if(yang!=0){ - newpoint.z=oldpoint.z*cos(yang)-oldpoint.x*sin(yang); - newpoint.x=oldpoint.z*sin(yang)+oldpoint.x*cos(yang); - oldpoint.z=newpoint.z; - oldpoint.x=newpoint.x; - } - - if(zang!=0){ - newpoint.x=oldpoint.x*cos(zang)-oldpoint.y*sin(zang); - newpoint.y=oldpoint.y*cos(zang)+oldpoint.x*sin(zang); - oldpoint.x=newpoint.x; - oldpoint.y=newpoint.y; - } - - if(xang!=0){ - newpoint.y=oldpoint.y*cos(xang)-oldpoint.z*sin(xang); - newpoint.z=oldpoint.y*sin(xang)+oldpoint.z*cos(xang); - oldpoint.z=newpoint.z; - oldpoint.y=newpoint.y; - } - - return oldpoint; - -} diff --git a/src/Quaternions.h b/src/Quaternions.h index 1c4fc95..c41dfe8 100644 --- a/src/Quaternions.h +++ b/src/Quaternions.h @@ -1,30 +1,7 @@ - #ifndef _QUATERNIONS_H_ #define _QUATERNIONS_H_ -#include <GL/gl.h> -#include "PhysicsMath.h" - -/**> Quaternion Structures <**/ -#define PI 3.14159265355555897932384626 -#define RADIANS 0 -#define DEGREES 1 -#define deg2rad .0174532925 - using namespace std; -typedef float Matrix_t [4][4]; -struct euler -{ - float x, y, z; -}; -struct angle_axis -{ - float x, y, z, angle; -}; -struct quaternion -{ - float x, y, z, w; -}; class XYZ{ public: @@ -42,42 +19,21 @@ class XYZ{ void operator*=(XYZ add); void operator/=(float add); void operator=(float add); - void vec(Vector add); bool operator==(XYZ add); }; -/*********************> Quaternion Function definition <********/ -quaternion To_Quat(int Degree_Flag, euler Euler); -quaternion To_Quat(angle_axis Ang_Ax); -quaternion To_Quat(Matrix_t m); -angle_axis Quat_2_AA(quaternion Quat); -void Quat_2_Matrix(quaternion Quat, Matrix_t m); -quaternion Normalize(quaternion Quat); -quaternion Quat_Mult(quaternion q1, quaternion q2); -quaternion QNormalize(quaternion Quat); -XYZ Quat2Vector(quaternion Quat); - void CrossProduct(XYZ P, XYZ Q, XYZ *V); void Normalise(XYZ *vectory); -float normaldotproduct(XYZ point1, XYZ point2); bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3); -bool LineFacet(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p); -float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ *p); float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc,XYZ n, XYZ *p); float LineFacetd(XYZ *p1,XYZ *p2,XYZ *pa,XYZ *pb,XYZ *pc,XYZ *n, XYZ *p); -bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33); -bool LineFacet(Vector p1,Vector p2,Vector pa,Vector pb,Vector pc,Vector *p); void ReflectVector(XYZ *vel, XYZ *n); XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang); -XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang); float findDistance(XYZ point1, XYZ point2); -float findLength(XYZ point1); float findLengthfast(XYZ point1); float findDistancefast(XYZ point1, XYZ point2); float dotproduct(XYZ point1, XYZ point2); -bool sphere_line_intersection ( - float x1, float y1 , float z1, - float x2, float y2 , float z2, - float x3, float y3 , float z3, float r ); +bool sphere_line_intersection(float x1, float y1, float z1, + float x2, float y2, float z2, float x3, float y3, float z3, float r); #endif diff --git a/src/Skeleton.cpp b/src/Skeleton.cpp index ae194d3..e5cdd7a 100644 --- a/src/Skeleton.cpp +++ b/src/Skeleton.cpp @@ -1,4 +1,5 @@ -/**> HEADER FILES <**/ +#include <cmath> + #include "Camera.h" #include "Skeleton.h" #include "misc.h" diff --git a/src/Sprites.cpp b/src/Sprites.cpp index 68a79dc..e3ffc75 100644 --- a/src/Sprites.cpp +++ b/src/Sprites.cpp @@ -1,3 +1,5 @@ +#include <cmath> + #include "Camera.h" #include "Fog.h" #include "Models.h" |