1 #ifndef _PHYSICSMATH_H_
2 #define _PHYSICSMATH_H_
6 #include "MacCompatibility.h"
8 //------------------------------------------------------------------------//
10 //------------------------------------------------------------------------//
12 float const pi = 3.14159265f;
13 float const g = -32.174f; // acceleration due to gravity, ft/s^2
14 float const rho = 0.0023769f; // desity of air at sea level, slugs/ft^3
15 float const tol = 0.0000000001f; // float type tolerance
18 //------------------------------------------------------------------------//
20 //------------------------------------------------------------------------//
21 inline float DegreesToRadians(float deg);
22 inline float RadiansToDegrees(float rad);
24 inline float DegreesToRadians(float deg)
26 return deg * pi / 180.0f;
29 inline float RadiansToDegrees(float rad)
31 return rad * 180.0f / pi;
34 //------------------------------------------------------------------------//
35 // Vector Class and vector functions
36 //------------------------------------------------------------------------//
44 Vector(float xi, float yi, float zi);
46 float Magnitude(void);
50 Vector& operator+=(Vector u); // vector addition
51 Vector& operator-=(Vector u); // vector subtraction
52 Vector& operator*=(float s); // scalar multiply
53 Vector& operator/=(float s); // scalar divide
55 Vector operator-(void);
59 inline Vector operator+(Vector u, Vector v);
60 inline Vector operator-(Vector u, Vector v);
61 inline Vector operator^(Vector u, Vector v);
62 inline float operator*(Vector u, Vector v);
63 inline Vector operator*(float s, Vector u);
64 inline Vector operator*(Vector u, float s);
65 inline Vector operator/(Vector u, float s);
66 inline float TripleScalarProduct(Vector u, Vector v, Vector w);
68 float fast_sqrt2 (register float arg);
69 float fast_sqrt2 (register float arg)
71 // Can replace with slower return std::sqrt(arg);
72 register float result;
74 if (arg == 0.0) return 0.0;
77 frsqrte result,arg // Calculate Square root
80 // Newton Rhapson iterations.
81 result = result + 0.5 * result * (1.0 - arg * result * result);
82 result = result + 0.5 * result * (1.0 - arg * result * result);
87 inline Vector::Vector(void)
94 inline Vector::Vector(float xi, float yi, float zi)
101 inline float Vector::Magnitude(void)
103 return (float) sqrt(x*x + y*y + z*z);
106 inline void Vector::Normalize(void)
108 float m = (float) sqrt(x*x + y*y + z*z);
114 if (fabs(x) < tol) x = 0.0f;
115 if (fabs(y) < tol) y = 0.0f;
116 if (fabs(z) < tol) z = 0.0f;
119 inline void Vector::Reverse(void)
126 inline Vector& Vector::operator+=(Vector u)
134 inline Vector& Vector::operator-=(Vector u)
142 inline Vector& Vector::operator*=(float s)
150 inline Vector& Vector::operator/=(float s)
158 inline Vector Vector::operator-(void)
160 return Vector(-x, -y, -z);
164 inline Vector operator+(Vector u, Vector v)
166 return Vector(u.x + v.x, u.y + v.y, u.z + v.z);
169 inline Vector operator-(Vector u, Vector v)
171 return Vector(u.x - v.x, u.y - v.y, u.z - v.z);
174 // Vector cross product (u cross v)
175 inline Vector operator^(Vector u, Vector v)
177 return Vector( u.y*v.z - u.z*v.y,
182 // Vector dot product
183 inline float operator*(Vector u, Vector v)
185 return (u.x*v.x + u.y*v.y + u.z*v.z);
188 inline Vector operator*(float s, Vector u)
190 return Vector(u.x*s, u.y*s, u.z*s);
193 inline Vector operator*(Vector u, float s)
195 return Vector(u.x*s, u.y*s, u.z*s);
198 inline Vector operator/(Vector u, float s)
200 return Vector(u.x/s, u.y/s, u.z/s);
203 // triple scalar product (u dot (v cross w))
204 inline float TripleScalarProduct(Vector u, Vector v, Vector w)
206 return float( (u.x * (v.y*w.z - v.z*w.y)) +
207 (u.y * (-v.x*w.z + v.z*w.x)) +
208 (u.z * (v.x*w.y - v.y*w.x)) );
215 //------------------------------------------------------------------------//
216 // Matrix Class and matrix functions
217 //------------------------------------------------------------------------//
221 // elements eij: i -> row, j -> column
222 float e11, e12, e13, e21, e22, e23, e31, e32, e33;
225 Matrix3x3( float r1c1, float r1c2, float r1c3,
226 float r2c1, float r2c2, float r2c3,
227 float r3c1, float r3c2, float r3c3 );
230 Matrix3x3 Transpose(void);
231 Matrix3x3 Inverse(void);
233 Matrix3x3& operator+=(Matrix3x3 m);
234 Matrix3x3& operator-=(Matrix3x3 m);
235 Matrix3x3& operator*=(float s);
236 Matrix3x3& operator/=(float s);
239 inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2);
240 inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2);
241 inline Matrix3x3 operator/(Matrix3x3 m, float s);
242 inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2);
243 inline Matrix3x3 operator*(Matrix3x3 m, float s);
244 inline Matrix3x3 operator*(float s, Matrix3x3 m);
245 inline Vector operator*(Matrix3x3 m, Vector u);
246 inline Vector operator*(Vector u, Matrix3x3 m);
252 inline Matrix3x3::Matrix3x3(void)
265 inline Matrix3x3::Matrix3x3( float r1c1, float r1c2, float r1c3,
266 float r2c1, float r2c2, float r2c3,
267 float r3c1, float r3c2, float r3c3 )
280 inline float Matrix3x3::det(void)
290 inline Matrix3x3 Matrix3x3::Transpose(void)
292 return Matrix3x3(e11,e21,e31,e12,e22,e32,e13,e23,e33);
295 inline Matrix3x3 Matrix3x3::Inverse(void)
297 float d = e11*e22*e33 -
306 return Matrix3x3( (e22*e33-e23*e32)/d,
307 -(e12*e33-e13*e32)/d,
309 -(e21*e33-e23*e31)/d,
311 -(e11*e23-e13*e21)/d,
313 -(e11*e32-e12*e31)/d,
314 (e11*e22-e12*e21)/d );
317 inline Matrix3x3& Matrix3x3::operator+=(Matrix3x3 m)
331 inline Matrix3x3& Matrix3x3::operator-=(Matrix3x3 m)
345 inline Matrix3x3& Matrix3x3::operator*=(float s)
359 inline Matrix3x3& Matrix3x3::operator/=(float s)
373 inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2)
375 return Matrix3x3( m1.e11+m2.e11,
386 inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2)
388 return Matrix3x3( m1.e11-m2.e11,
399 inline Matrix3x3 operator/(Matrix3x3 m, float s)
401 return Matrix3x3( m.e11/s,
412 inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2)
414 return Matrix3x3( m1.e11*m2.e11 + m1.e12*m2.e21 + m1.e13*m2.e31,
415 m1.e11*m2.e12 + m1.e12*m2.e22 + m1.e13*m2.e32,
416 m1.e11*m2.e13 + m1.e12*m2.e23 + m1.e13*m2.e33,
417 m1.e21*m2.e11 + m1.e22*m2.e21 + m1.e23*m2.e31,
418 m1.e21*m2.e12 + m1.e22*m2.e22 + m1.e23*m2.e32,
419 m1.e21*m2.e13 + m1.e22*m2.e23 + m1.e23*m2.e33,
420 m1.e31*m2.e11 + m1.e32*m2.e21 + m1.e33*m2.e31,
421 m1.e31*m2.e12 + m1.e32*m2.e22 + m1.e33*m2.e32,
422 m1.e31*m2.e13 + m1.e32*m2.e23 + m1.e33*m2.e33 );
425 inline Matrix3x3 operator*(Matrix3x3 m, float s)
427 return Matrix3x3( m.e11*s,
438 inline Matrix3x3 operator*(float s, Matrix3x3 m)
440 return Matrix3x3( m.e11*s,
451 inline Vector operator*(Matrix3x3 m, Vector u)
453 return Vector( m.e11*u.x + m.e12*u.y + m.e13*u.z,
454 m.e21*u.x + m.e22*u.y + m.e23*u.z,
455 m.e31*u.x + m.e32*u.y + m.e33*u.z);
458 inline Vector operator*(Vector u, Matrix3x3 m)
460 return Vector( u.x*m.e11 + u.y*m.e21 + u.z*m.e31,
461 u.x*m.e12 + u.y*m.e22 + u.z*m.e32,
462 u.x*m.e13 + u.y*m.e23 + u.z*m.e33);
465 //------------------------------------------------------------------------//
466 // Quaternion Class and Quaternion functions
467 //------------------------------------------------------------------------//
471 float n; // number (scalar) part
472 Vector v; // vector part: v.x, v.y, v.z
475 Quaternion(float e0, float e1, float e2, float e3);
477 float Magnitude(void);
478 Vector GetVector(void);
479 float GetScalar(void);
480 Quaternion operator+=(Quaternion q);
481 Quaternion operator-=(Quaternion q);
482 Quaternion operator*=(float s);
483 Quaternion operator/=(float s);
484 Quaternion operator~(void) const { return Quaternion(n, -v.x, -v.y, -v.z);}
487 inline Quaternion operator+(Quaternion q1, Quaternion q2);
488 inline Quaternion operator-(Quaternion q1, Quaternion q2);
489 inline Quaternion operator*(Quaternion q1, Quaternion q2);
490 inline Quaternion operator*(Quaternion q, float s);
491 inline Quaternion operator*(float s, Quaternion q);
492 inline Quaternion operator*(Quaternion q, Vector v);
493 inline Quaternion operator*(Vector v, Quaternion q);
494 inline Quaternion operator/(Quaternion q, float s);
495 inline float QGetAngle(Quaternion q);
496 inline Vector QGetAxis(Quaternion q);
497 inline Quaternion QRotate(Quaternion q1, Quaternion q2);
498 inline Vector QVRotate(Quaternion q, Vector v);
499 inline Quaternion MakeQFromEulerAngles(float x, float y, float z);
500 inline Vector MakeEulerAnglesFromQ(Quaternion q);
503 inline Quaternion::Quaternion(void)
511 inline Quaternion::Quaternion(float e0, float e1, float e2, float e3)
519 inline float Quaternion::Magnitude(void)
521 return (float) sqrt(n*n + v.x*v.x + v.y*v.y + v.z*v.z);
524 inline Vector Quaternion::GetVector(void)
526 return Vector(v.x, v.y, v.z);
529 inline float Quaternion::GetScalar(void)
534 inline Quaternion Quaternion::operator+=(Quaternion q)
543 inline Quaternion Quaternion::operator-=(Quaternion q)
552 inline Quaternion Quaternion::operator*=(float s)
561 inline Quaternion Quaternion::operator/=(float s)
570 /*inline Quaternion Quaternion::operator~()
572 return Quaternion(n, -v.x, -v.y, -v.z);
575 inline Quaternion operator+(Quaternion q1, Quaternion q2)
577 return Quaternion( q1.n + q2.n,
583 inline Quaternion operator-(Quaternion q1, Quaternion q2)
585 return Quaternion( q1.n - q2.n,
591 inline Quaternion operator*(Quaternion q1, Quaternion q2)
593 return Quaternion( q1.n*q2.n - q1.v.x*q2.v.x - q1.v.y*q2.v.y - q1.v.z*q2.v.z,
594 q1.n*q2.v.x + q1.v.x*q2.n + q1.v.y*q2.v.z - q1.v.z*q2.v.y,
595 q1.n*q2.v.y + q1.v.y*q2.n + q1.v.z*q2.v.x - q1.v.x*q2.v.z,
596 q1.n*q2.v.z + q1.v.z*q2.n + q1.v.x*q2.v.y - q1.v.y*q2.v.x);
599 inline Quaternion operator*(Quaternion q, float s)
601 return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s);
604 inline Quaternion operator*(float s, Quaternion q)
606 return Quaternion(q.n*s, q.v.x*s, q.v.y*s, q.v.z*s);
609 inline Quaternion operator*(Quaternion q, Vector v)
611 return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z),
612 q.n*v.x + q.v.y*v.z - q.v.z*v.y,
613 q.n*v.y + q.v.z*v.x - q.v.x*v.z,
614 q.n*v.z + q.v.x*v.y - q.v.y*v.x);
617 inline Quaternion operator*(Vector v, Quaternion q)
619 return Quaternion( -(q.v.x*v.x + q.v.y*v.y + q.v.z*v.z),
620 q.n*v.x + q.v.z*v.y - q.v.y*v.z,
621 q.n*v.y + q.v.x*v.z - q.v.z*v.x,
622 q.n*v.z + q.v.y*v.x - q.v.x*v.y);
625 inline Quaternion operator/(Quaternion q, float s)
627 return Quaternion(q.n/s, q.v.x/s, q.v.y/s, q.v.z/s);
630 inline float QGetAngle(Quaternion q)
632 return (float) (2*acosf(q.n));
635 inline Vector QGetAxis(Quaternion q)
649 inline Quaternion QRotate(Quaternion q1, Quaternion q2)
654 inline Vector QVRotate(Quaternion q, Vector v)
661 return t.GetVector();
664 inline Quaternion MakeQFromEulerAngles(float x, float y, float z)
667 double roll = DegreesToRadians(x);
668 double pitch = DegreesToRadians(y);
669 double yaw = DegreesToRadians(z);
671 double cyaw, cpitch, croll, syaw, spitch, sroll;
672 double cyawcpitch, syawspitch, cyawspitch, syawcpitch;
674 cyaw = cos(0.5f * yaw);
675 cpitch = cos(0.5f * pitch);
676 croll = cos(0.5f * roll);
677 syaw = sin(0.5f * yaw);
678 spitch = sin(0.5f * pitch);
679 sroll = sin(0.5f * roll);
681 cyawcpitch = cyaw*cpitch;
682 syawspitch = syaw*spitch;
683 cyawspitch = cyaw*spitch;
684 syawcpitch = syaw*cpitch;
686 q.n = (float) (cyawcpitch * croll + syawspitch * sroll);
687 q.v.x = (float) (cyawcpitch * sroll - syawspitch * croll);
688 q.v.y = (float) (cyawspitch * croll + syawcpitch * sroll);
689 q.v.z = (float) (syawcpitch * croll - cyawspitch * sroll);
694 inline Vector MakeEulerAnglesFromQ(Quaternion q)
696 double r11, r21, r31, r32, r33, r12, r13;
697 double q00, q11, q22, q33;
706 r11 = q00 + q11 - q22 - q33;
707 r21 = 2 * (q.v.x*q.v.y + q.n*q.v.z);
708 r31 = 2 * (q.v.x*q.v.z - q.n*q.v.y);
709 r32 = 2 * (q.v.y*q.v.z + q.n*q.v.x);
710 r33 = q00 - q11 - q22 + q33;
715 r12 = 2 * (q.v.x*q.v.y - q.n*q.v.z);
716 r13 = 2 * (q.v.x*q.v.z + q.n*q.v.y);
718 u.x = RadiansToDegrees(0.0f); //roll
719 u.y = RadiansToDegrees((float) (-(pi/2) * r31/tmp)); // pitch
720 u.z = RadiansToDegrees((float) atan2(-r12, -r31*r13)); // yaw
724 u.x = RadiansToDegrees((float) atan2(r32, r33)); // roll
725 u.y = RadiansToDegrees((float) asinf(-r31)); // pitch
726 u.z = RadiansToDegrees((float) atan2(r21, r11)); // yaw