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/>.
21 #ifndef _QUATERNIONS_HPP_
22 #define _QUATERNIONS_HPP_
24 #include "Graphic/gamegl.hpp"
34 XYZ() : x(0.0f), y(0.0f), z(0.0f) {}
35 inline XYZ operator+(XYZ add);
36 inline XYZ operator-(XYZ add);
37 inline XYZ operator*(float add);
38 inline XYZ operator*(XYZ add);
39 inline XYZ operator/(float add);
40 inline void operator+=(XYZ add);
41 inline void operator-=(XYZ add);
42 inline void operator*=(float add);
43 inline void operator*=(XYZ add);
44 inline void operator/=(float add);
45 inline void operator=(float add);
46 inline bool operator==(XYZ add);
49 inline void CrossProduct(XYZ *P, XYZ *Q, XYZ *V);
50 inline void CrossProduct(XYZ P, XYZ Q, XYZ *V);
51 inline void Normalise(XYZ *vectory);
52 inline float normaldotproduct(XYZ point1, XYZ point2);
53 inline float fast_sqrt (register float arg);
54 bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3);
55 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p);
56 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p);
57 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p);
58 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p);
59 float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p);
60 inline void ReflectVector(XYZ *vel, const XYZ *n);
61 inline void ReflectVector(XYZ *vel, const XYZ &n);
62 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang);
63 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang);
64 inline float findDistance(XYZ *point1, XYZ *point2);
65 inline float findLength(XYZ *point1);
66 inline float findLengthfast(XYZ *point1);
67 inline float distsq(XYZ *point1, XYZ *point2);
68 inline float distsq(XYZ point1, XYZ point2);
69 inline float distsqflat(XYZ *point1, XYZ *point2);
70 inline float dotproduct(const XYZ *point1, const XYZ *point2);
71 bool sphere_line_intersection (
72 float x1, float y1 , float z1,
73 float x2, float y2 , float z2,
74 float x3, float y3 , float z3, float r );
75 bool sphere_line_intersection (
76 XYZ *p1, XYZ *p2, XYZ *p3, float *r );
77 inline bool DistancePointLine( XYZ *Point, XYZ *LineStart, XYZ *LineEnd, float *Distance, XYZ *Intersection );
80 inline void Normalise(XYZ *vectory)
83 d = fast_sqrt(vectory->x * vectory->x + vectory->y * vectory->y + vectory->z * vectory->z);
92 inline XYZ XYZ::operator+(XYZ add)
102 inline XYZ XYZ::operator-(XYZ add)
112 inline XYZ XYZ::operator*(float add)
121 inline XYZ XYZ::operator*(XYZ add)
130 inline XYZ XYZ::operator/(float add)
139 inline void XYZ::operator+=(XYZ add)
146 inline void XYZ::operator-=(XYZ add)
153 inline void XYZ::operator*=(float add)
160 inline void XYZ::operator*=(XYZ add)
167 inline void XYZ::operator/=(float add)
174 inline void XYZ::operator=(float add)
181 inline bool XYZ::operator==(XYZ add)
183 if (x == add.x && y == add.y && z == add.z)
188 inline void CrossProduct(XYZ *P, XYZ *Q, XYZ *V)
190 V->x = P->y * Q->z - P->z * Q->y;
191 V->y = P->z * Q->x - P->x * Q->z;
192 V->z = P->x * Q->y - P->y * Q->x;
195 inline void CrossProduct(XYZ P, XYZ Q, XYZ *V)
197 V->x = P.y * Q.z - P.z * Q.y;
198 V->y = P.z * Q.x - P.x * Q.z;
199 V->z = P.x * Q.y - P.y * Q.x;
202 inline float fast_sqrt (register float arg)
207 inline float normaldotproduct(XYZ point1, XYZ point2)
209 static GLfloat returnvalue;
212 returnvalue = (point1.x * point2.x + point1.y * point2.y + point1.z * point2.z);
216 inline void ReflectVector(XYZ *vel, const XYZ *n)
218 ReflectVector(vel, *n);
221 inline void ReflectVector(XYZ *vel, const XYZ &n)
225 static float dotprod;
227 dotprod = dotproduct(&n, vel);
228 vn.x = n.x * dotprod;
229 vn.y = n.y * dotprod;
230 vn.z = n.z * dotprod;
232 vt.x = vel->x - vn.x;
233 vt.y = vel->y - vn.y;
234 vt.z = vel->z - vn.z;
236 vel->x = vt.x - vn.x;
237 vel->y = vt.y - vn.y;
238 vel->z = vt.z - vn.z;
241 inline float dotproduct(const XYZ *point1, const XYZ *point2)
243 static GLfloat returnvalue;
244 returnvalue = (point1->x * point2->x + point1->y * point2->y + point1->z * point2->z);
248 inline float findDistance(XYZ *point1, XYZ *point2)
250 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)));
253 inline float findLength(XYZ *point1)
255 return(fast_sqrt((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z)));
259 inline float findLengthfast(XYZ *point1)
261 return((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z));
264 inline float distsq(XYZ *point1, XYZ *point2)
266 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));
269 inline float distsq(XYZ point1, XYZ point2)
271 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));
274 inline float distsqflat(XYZ *point1, XYZ *point2)
276 return((point1->x - point2->x) * (point1->x - point2->x) + (point1->z - point2->z) * (point1->z - point2->z));
279 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang)
297 newpoint.z = thePoint.z * cosf(yang) - thePoint.x * sinf(yang);
298 newpoint.x = thePoint.z * sinf(yang) + thePoint.x * cosf(yang);
299 thePoint.z = newpoint.z;
300 thePoint.x = newpoint.x;
304 newpoint.x = thePoint.x * cosf(zang) - thePoint.y * sinf(zang);
305 newpoint.y = thePoint.y * cosf(zang) + thePoint.x * sinf(zang);
306 thePoint.x = newpoint.x;
307 thePoint.y = newpoint.y;
311 newpoint.y = thePoint.y * cosf(xang) - thePoint.z * sinf(xang);
312 newpoint.z = thePoint.y * sinf(xang) + thePoint.z * cosf(xang);
313 thePoint.z = newpoint.z;
314 thePoint.y = newpoint.y;
320 inline float square( float f )
325 inline bool sphere_line_intersection (
326 float x1, float y1 , float z1,
327 float x2, float y2 , float z2,
328 float x3, float y3 , float z3, float r )
331 // x1,y1,z1 P1 coordinates (point of line)
332 // x2,y2,z2 P2 coordinates (point of line)
333 // x3,y3,z3, r P3 coordinates and radius (sphere)
334 // x,y,z intersection coordinates
336 // This function returns a pointer array which first index indicates
337 // the number of intersection point, followed by coordinate pairs.
339 //~ static float x , y , z;
340 static float a, b, c, /*mu,*/ i ;
342 if (x1 > x3 + r && x2 > x3 + r) return(0);
343 if (x1 < x3 - r && x2 < x3 - r) return(0);
344 if (y1 > y3 + r && y2 > y3 + r) return(0);
345 if (y1 < y3 - r && y2 < y3 - r) return(0);
346 if (z1 > z3 + r && z2 > z3 + r) return(0);
347 if (z1 < z3 - r && z2 < z3 - r) return(0);
348 a = square(x2 - x1) + square(y2 - y1) + square(z2 - z1);
349 b = 2 * ( (x2 - x1) * (x1 - x3)
350 + (y2 - y1) * (y1 - y3)
351 + (z2 - z1) * (z1 - z3) ) ;
352 c = square(x3) + square(y3) +
353 square(z3) + square(x1) +
354 square(y1) + square(z1) -
355 2 * ( x3 * x1 + y3 * y1 + z3 * z1 ) - square(r) ;
356 i = b * b - 4 * a * c ;
365 inline bool sphere_line_intersection (
366 XYZ *p1, XYZ *p2, XYZ *p3, float *r )
369 // x1,p1->y,p1->z P1 coordinates (point of line)
370 // p2->x,p2->y,p2->z P2 coordinates (point of line)
371 // p3->x,p3->y,p3->z, r P3 coordinates and radius (sphere)
372 // x,y,z intersection coordinates
374 // This function returns a pointer array which first index indicates
375 // the number of intersection point, followed by coordinate pairs.
377 //~ static float x , y , z;
378 static float a, b, c, /*mu,*/ i ;
380 if (p1->x > p3->x + *r && p2->x > p3->x + *r) return(0);
381 if (p1->x < p3->x - *r && p2->x < p3->x - *r) return(0);
382 if (p1->y > p3->y + *r && p2->y > p3->y + *r) return(0);
383 if (p1->y < p3->y - *r && p2->y < p3->y - *r) return(0);
384 if (p1->z > p3->z + *r && p2->z > p3->z + *r) return(0);
385 if (p1->z < p3->z - *r && p2->z < p3->z - *r) return(0);
386 a = square(p2->x - p1->x) + square(p2->y - p1->y) + square(p2->z - p1->z);
387 b = 2 * ( (p2->x - p1->x) * (p1->x - p3->x)
388 + (p2->y - p1->y) * (p1->y - p3->y)
389 + (p2->z - p1->z) * (p1->z - p3->z) ) ;
390 c = square(p3->x) + square(p3->y) +
391 square(p3->z) + square(p1->x) +
392 square(p1->y) + square(p1->z) -
393 2 * ( p3->x * p1->x + p3->y * p1->y + p3->z * p1->z ) - square(*r) ;
394 i = b * b - 4 * a * c ;
403 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang)
411 newpoint.z = oldpoint.z * cosf(yang) - oldpoint.x * sinf(yang);
412 newpoint.x = oldpoint.z * sinf(yang) + oldpoint.x * cosf(yang);
413 oldpoint.z = newpoint.z;
414 oldpoint.x = newpoint.x;
418 newpoint.x = oldpoint.x * cosf(zang) - oldpoint.y * sinf(zang);
419 newpoint.y = oldpoint.y * cosf(zang) + oldpoint.x * sinf(zang);
420 oldpoint.x = newpoint.x;
421 oldpoint.y = newpoint.y;
425 newpoint.y = oldpoint.y * cosf(xang) - oldpoint.z * sinf(xang);
426 newpoint.z = oldpoint.y * sinf(xang) + oldpoint.z * cosf(xang);
427 oldpoint.z = newpoint.z;
428 oldpoint.y = newpoint.y;
435 inline bool DistancePointLine( XYZ *Point, XYZ *LineStart, XYZ *LineEnd, float *Distance, XYZ *Intersection )
440 LineMag = findDistance( LineEnd, LineStart );
442 U = ( ( ( Point->x - LineStart->x ) * ( LineEnd->x - LineStart->x ) ) +
443 ( ( Point->y - LineStart->y ) * ( LineEnd->y - LineStart->y ) ) +
444 ( ( Point->z - LineStart->z ) * ( LineEnd->z - LineStart->z ) ) ) /
445 ( LineMag * LineMag );
447 if ( U < 0.0f || U > 1.0f )
448 return 0; // closest point does not fall within the line segment
450 Intersection->x = LineStart->x + U * ( LineEnd->x - LineStart->x );
451 Intersection->y = LineStart->y + U * ( LineEnd->y - LineStart->y );
452 Intersection->z = LineStart->z + U * ( LineEnd->z - LineStart->z );
454 *Distance = findDistance( Point, Intersection );