2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru 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 Lugaru 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 Lugaru. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef _QUATERNIONS_H_
23 #define _QUATERNIONS_H_
26 #include "PhysicsMath.h"
29 /**> Quaternion Structures <**/
30 #define PI 3.14159265355555897932384626
33 #define deg2rad .0174532925
35 //using namespace std;
36 typedef float Matrix_t [4][4];
53 XYZ() : x(0.0f), y(0.0f), z(0.0f) {}
54 inline XYZ operator+(XYZ add);
55 inline XYZ operator-(XYZ add);
56 inline XYZ operator*(float add);
57 inline XYZ operator*(XYZ add);
58 inline XYZ operator/(float add);
59 inline void operator+=(XYZ add);
60 inline void operator-=(XYZ add);
61 inline void operator*=(float add);
62 inline void operator*=(XYZ add);
63 inline void operator/=(float add);
64 inline void operator=(float add);
65 inline void vec(Vector add);
66 inline bool operator==(XYZ add);
69 /*********************> Quaternion Function definition <********/
70 quaternion To_Quat(int Degree_Flag, euler Euler);
71 quaternion To_Quat(angle_axis Ang_Ax);
72 quaternion To_Quat(Matrix_t m);
73 angle_axis Quat_2_AA(quaternion Quat);
74 void Quat_2_Matrix(quaternion Quat, Matrix_t m);
75 quaternion Normalize(quaternion Quat);
76 quaternion Quat_Mult(quaternion q1, quaternion q2);
77 quaternion QNormalize(quaternion Quat);
78 XYZ Quat2Vector(quaternion Quat);
80 inline void CrossProduct(XYZ *P, XYZ *Q, XYZ *V);
81 inline void CrossProduct(XYZ P, XYZ Q, XYZ *V);
82 inline void Normalise(XYZ *vectory);
83 inline float normaldotproduct(XYZ point1, XYZ point2);
84 inline float fast_sqrt (register float arg);
85 bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3);
86 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p);
87 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p);
88 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p);
89 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p);
90 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p);
91 bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33);
92 bool LineFacet(Vector p1, Vector p2, Vector pa, Vector pb, Vector pc, Vector *p);
93 inline void ReflectVector(XYZ *vel, const XYZ *n);
94 inline void ReflectVector(XYZ *vel, const XYZ &n);
95 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang);
96 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang);
97 inline float findDistance(XYZ *point1, XYZ *point2);
98 inline float findLength(XYZ *point1);
99 inline float findLengthfast(XYZ *point1);
100 inline float distsq(XYZ *point1, XYZ *point2);
101 inline float distsq(XYZ point1, XYZ point2);
102 inline float distsqflat(XYZ *point1, XYZ *point2);
103 inline float dotproduct(const XYZ *point1, const XYZ *point2);
104 bool sphere_line_intersection (
105 float x1, float y1 , float z1,
106 float x2, float y2 , float z2,
107 float x3, float y3 , float z3, float r );
108 bool sphere_line_intersection (
109 XYZ *p1, XYZ *p2, XYZ *p3, float *r );
110 inline bool DistancePointLine( XYZ *Point, XYZ *LineStart, XYZ *LineEnd, float *Distance, XYZ *Intersection );
113 inline void Normalise(XYZ *vectory)
116 d = fast_sqrt(vectory->x * vectory->x + vectory->y * vectory->y + vectory->z * vectory->z);
125 inline XYZ XYZ::operator+(XYZ add)
135 inline XYZ XYZ::operator-(XYZ add)
145 inline XYZ XYZ::operator*(float add)
154 inline XYZ XYZ::operator*(XYZ add)
163 inline XYZ XYZ::operator/(float add)
172 inline void XYZ::operator+=(XYZ add)
179 inline void XYZ::operator-=(XYZ add)
186 inline void XYZ::operator*=(float add)
193 inline void XYZ::operator*=(XYZ add)
200 inline void XYZ::operator/=(float add)
207 inline void XYZ::operator=(float add)
214 inline void XYZ::vec(Vector add)
221 inline bool XYZ::operator==(XYZ add)
223 if (x == add.x && y == add.y && z == add.z)
228 inline void CrossProduct(XYZ *P, XYZ *Q, XYZ *V)
230 V->x = P->y * Q->z - P->z * Q->y;
231 V->y = P->z * Q->x - P->x * Q->z;
232 V->z = P->x * Q->y - P->y * Q->x;
235 inline void CrossProduct(XYZ P, XYZ Q, XYZ *V)
237 V->x = P.y * Q.z - P.z * Q.y;
238 V->y = P.z * Q.x - P.x * Q.z;
239 V->z = P.x * Q.y - P.y * Q.x;
242 inline float fast_sqrt (register float arg)
247 inline float normaldotproduct(XYZ point1, XYZ point2)
249 static GLfloat returnvalue;
252 returnvalue = (point1.x * point2.x + point1.y * point2.y + point1.z * point2.z);
256 inline void ReflectVector(XYZ *vel, const XYZ *n)
258 ReflectVector(vel, *n);
261 inline void ReflectVector(XYZ *vel, const XYZ &n)
265 static float dotprod;
267 dotprod = dotproduct(&n, vel);
268 vn.x = n.x * dotprod;
269 vn.y = n.y * dotprod;
270 vn.z = n.z * dotprod;
272 vt.x = vel->x - vn.x;
273 vt.y = vel->y - vn.y;
274 vt.z = vel->z - vn.z;
276 vel->x = vt.x - vn.x;
277 vel->y = vt.y - vn.y;
278 vel->z = vt.z - vn.z;
281 inline float dotproduct(const XYZ *point1, const XYZ *point2)
283 static GLfloat returnvalue;
284 returnvalue = (point1->x * point2->x + point1->y * point2->y + point1->z * point2->z);
288 inline float findDistance(XYZ *point1, XYZ *point2)
290 return(fast_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)));
293 inline float findLength(XYZ *point1)
295 return(fast_sqrt((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z)));
299 inline float findLengthfast(XYZ *point1)
301 return((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z));
304 inline float distsq(XYZ *point1, XYZ *point2)
306 return((point1->x - point2->x) * (point1->x - point2->x) + (point1->y - point2->y) * (point1->y - point2->y) + (point1->z - point2->z) * (point1->z - point2->z));
309 inline float distsq(XYZ point1, XYZ point2)
311 return((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y) + (point1.z - point2.z) * (point1.z - point2.z));
314 inline float distsqflat(XYZ *point1, XYZ *point2)
316 return((point1->x - point2->x) * (point1->x - point2->x) + (point1->z - point2->z) * (point1->z - point2->z));
319 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang)
337 newpoint.z = thePoint.z * cosf(yang) - thePoint.x * sinf(yang);
338 newpoint.x = thePoint.z * sinf(yang) + thePoint.x * cosf(yang);
339 thePoint.z = newpoint.z;
340 thePoint.x = newpoint.x;
344 newpoint.x = thePoint.x * cosf(zang) - thePoint.y * sinf(zang);
345 newpoint.y = thePoint.y * cosf(zang) + thePoint.x * sinf(zang);
346 thePoint.x = newpoint.x;
347 thePoint.y = newpoint.y;
351 newpoint.y = thePoint.y * cosf(xang) - thePoint.z * sinf(xang);
352 newpoint.z = thePoint.y * sinf(xang) + thePoint.z * cosf(xang);
353 thePoint.z = newpoint.z;
354 thePoint.y = newpoint.y;
360 inline float square( float f )
365 inline bool sphere_line_intersection (
366 float x1, float y1 , float z1,
367 float x2, float y2 , float z2,
368 float x3, float y3 , float z3, float r )
371 // x1,y1,z1 P1 coordinates (point of line)
372 // x2,y2,z2 P2 coordinates (point of line)
373 // x3,y3,z3, r P3 coordinates and radius (sphere)
374 // x,y,z intersection coordinates
376 // This function returns a pointer array which first index indicates
377 // the number of intersection point, followed by coordinate pairs.
379 //~ static float x , y , z;
380 static float a, b, c, /*mu,*/ i ;
382 if (x1 > x3 + r && x2 > x3 + r) return(0);
383 if (x1 < x3 - r && x2 < x3 - r) return(0);
384 if (y1 > y3 + r && y2 > y3 + r) return(0);
385 if (y1 < y3 - r && y2 < y3 - r) return(0);
386 if (z1 > z3 + r && z2 > z3 + r) return(0);
387 if (z1 < z3 - r && z2 < z3 - r) return(0);
388 a = square(x2 - x1) + square(y2 - y1) + square(z2 - z1);
389 b = 2 * ( (x2 - x1) * (x1 - x3)
390 + (y2 - y1) * (y1 - y3)
391 + (z2 - z1) * (z1 - z3) ) ;
392 c = square(x3) + square(y3) +
393 square(z3) + square(x1) +
394 square(y1) + square(z1) -
395 2 * ( x3 * x1 + y3 * y1 + z3 * z1 ) - square(r) ;
396 i = b * b - 4 * a * c ;
405 inline bool sphere_line_intersection (
406 XYZ *p1, XYZ *p2, XYZ *p3, float *r )
409 // x1,p1->y,p1->z P1 coordinates (point of line)
410 // p2->x,p2->y,p2->z P2 coordinates (point of line)
411 // p3->x,p3->y,p3->z, r P3 coordinates and radius (sphere)
412 // x,y,z intersection coordinates
414 // This function returns a pointer array which first index indicates
415 // the number of intersection point, followed by coordinate pairs.
417 //~ static float x , y , z;
418 static float a, b, c, /*mu,*/ i ;
420 if (p1->x > p3->x + *r && p2->x > p3->x + *r) return(0);
421 if (p1->x < p3->x - *r && p2->x < p3->x - *r) return(0);
422 if (p1->y > p3->y + *r && p2->y > p3->y + *r) return(0);
423 if (p1->y < p3->y - *r && p2->y < p3->y - *r) return(0);
424 if (p1->z > p3->z + *r && p2->z > p3->z + *r) return(0);
425 if (p1->z < p3->z - *r && p2->z < p3->z - *r) return(0);
426 a = square(p2->x - p1->x) + square(p2->y - p1->y) + square(p2->z - p1->z);
427 b = 2 * ( (p2->x - p1->x) * (p1->x - p3->x)
428 + (p2->y - p1->y) * (p1->y - p3->y)
429 + (p2->z - p1->z) * (p1->z - p3->z) ) ;
430 c = square(p3->x) + square(p3->y) +
431 square(p3->z) + square(p1->x) +
432 square(p1->y) + square(p1->z) -
433 2 * ( p3->x * p1->x + p3->y * p1->y + p3->z * p1->z ) - square(*r) ;
434 i = b * b - 4 * a * c ;
443 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang)
451 newpoint.z = oldpoint.z * cosf(yang) - oldpoint.x * sinf(yang);
452 newpoint.x = oldpoint.z * sinf(yang) + oldpoint.x * cosf(yang);
453 oldpoint.z = newpoint.z;
454 oldpoint.x = newpoint.x;
458 newpoint.x = oldpoint.x * cosf(zang) - oldpoint.y * sinf(zang);
459 newpoint.y = oldpoint.y * cosf(zang) + oldpoint.x * sinf(zang);
460 oldpoint.x = newpoint.x;
461 oldpoint.y = newpoint.y;
465 newpoint.y = oldpoint.y * cosf(xang) - oldpoint.z * sinf(xang);
466 newpoint.z = oldpoint.y * sinf(xang) + oldpoint.z * cosf(xang);
467 oldpoint.z = newpoint.z;
468 oldpoint.y = newpoint.y;
475 inline bool DistancePointLine( XYZ *Point, XYZ *LineStart, XYZ *LineEnd, float *Distance, XYZ *Intersection )
480 LineMag = findDistance( LineEnd, LineStart );
482 U = ( ( ( Point->x - LineStart->x ) * ( LineEnd->x - LineStart->x ) ) +
483 ( ( Point->y - LineStart->y ) * ( LineEnd->y - LineStart->y ) ) +
484 ( ( Point->z - LineStart->z ) * ( LineEnd->z - LineStart->z ) ) ) /
485 ( LineMag * LineMag );
487 if ( U < 0.0f || U > 1.0f )
488 return 0; // closest point does not fall within the line segment
490 Intersection->x = LineStart->x + U * ( LineEnd->x - LineStart->x );
491 Intersection->y = LineStart->y + U * ( LineEnd->y - LineStart->y );
492 Intersection->z = LineStart->z + U * ( LineEnd->z - LineStart->z );
494 *Distance = findDistance( Point, Intersection );