2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - 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"
40 inline XYZ operator+(XYZ add);
41 inline XYZ operator-(XYZ add);
42 inline XYZ operator*(float add);
43 inline XYZ operator*(XYZ add);
44 inline XYZ operator/(float add);
45 inline void operator+=(XYZ add);
46 inline void operator-=(XYZ add);
47 inline void operator*=(float add);
48 inline void operator*=(XYZ add);
49 inline void operator/=(float add);
50 inline void operator=(float add);
51 inline bool operator==(XYZ add);
54 inline void CrossProduct(XYZ* P, XYZ* Q, XYZ* V);
55 inline void CrossProduct(XYZ P, XYZ Q, XYZ* V);
56 inline void Normalise(XYZ* vectory);
57 inline float normaldotproduct(XYZ point1, XYZ point2);
58 inline float fast_sqrt(register float arg);
59 bool PointInTriangle(XYZ* p, XYZ normal, XYZ* p1, XYZ* p2, XYZ* p3);
60 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ* p);
61 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ* p);
62 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ* p);
63 float LineFacetd(XYZ* p1, XYZ* p2, XYZ* pa, XYZ* pb, XYZ* pc, XYZ* n, XYZ* p);
64 float LineFacetd(XYZ* p1, XYZ* p2, XYZ* pa, XYZ* pb, XYZ* pc, XYZ* p);
65 inline void ReflectVector(XYZ* vel, const XYZ* n);
66 inline void ReflectVector(XYZ* vel, const XYZ& n);
67 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang);
68 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang);
69 inline float findDistance(XYZ* point1, XYZ* point2);
70 inline float findLength(XYZ* point1);
71 inline float findLengthfast(XYZ* point1);
72 inline float distsq(XYZ* point1, XYZ* point2);
73 inline float distsq(XYZ point1, XYZ point2);
74 inline float distsqflat(XYZ* point1, XYZ* point2);
75 inline float dotproduct(const XYZ* point1, const XYZ* point2);
76 bool sphere_line_intersection(
77 float x1, float y1, float z1,
78 float x2, float y2, float z2,
79 float x3, float y3, float z3, float r);
80 bool sphere_line_intersection(
81 XYZ* p1, XYZ* p2, XYZ* p3, float* r);
82 inline bool DistancePointLine(XYZ* Point, XYZ* LineStart, XYZ* LineEnd, float* Distance, XYZ* Intersection);
84 inline void Normalise(XYZ* vectory)
87 d = fast_sqrt(vectory->x * vectory->x + vectory->y * vectory->y + vectory->z * vectory->z);
96 inline XYZ XYZ::operator+(XYZ add)
106 inline XYZ XYZ::operator-(XYZ add)
116 inline XYZ XYZ::operator*(float add)
125 inline XYZ XYZ::operator*(XYZ add)
134 inline XYZ XYZ::operator/(float add)
143 inline void XYZ::operator+=(XYZ add)
150 inline void XYZ::operator-=(XYZ add)
157 inline void XYZ::operator*=(float add)
164 inline void XYZ::operator*=(XYZ add)
171 inline void XYZ::operator/=(float add)
178 inline void XYZ::operator=(float add)
185 inline bool XYZ::operator==(XYZ add)
187 if (x == add.x && y == add.y && z == add.z)
192 inline void CrossProduct(XYZ* P, XYZ* Q, XYZ* V)
194 V->x = P->y * Q->z - P->z * Q->y;
195 V->y = P->z * Q->x - P->x * Q->z;
196 V->z = P->x * Q->y - P->y * Q->x;
199 inline void CrossProduct(XYZ P, XYZ Q, XYZ* V)
201 V->x = P.y * Q.z - P.z * Q.y;
202 V->y = P.z * Q.x - P.x * Q.z;
203 V->z = P.x * Q.y - P.y * Q.x;
206 inline float fast_sqrt(register float arg)
211 inline float normaldotproduct(XYZ point1, XYZ point2)
213 static GLfloat returnvalue;
216 returnvalue = (point1.x * point2.x + point1.y * point2.y + point1.z * point2.z);
220 inline void ReflectVector(XYZ* vel, const XYZ* n)
222 ReflectVector(vel, *n);
225 inline void ReflectVector(XYZ* vel, const XYZ& n)
229 static float dotprod;
231 dotprod = dotproduct(&n, vel);
232 vn.x = n.x * dotprod;
233 vn.y = n.y * dotprod;
234 vn.z = n.z * dotprod;
236 vt.x = vel->x - vn.x;
237 vt.y = vel->y - vn.y;
238 vt.z = vel->z - vn.z;
240 vel->x = vt.x - vn.x;
241 vel->y = vt.y - vn.y;
242 vel->z = vt.z - vn.z;
245 inline float dotproduct(const XYZ* point1, const XYZ* point2)
247 static GLfloat returnvalue;
248 returnvalue = (point1->x * point2->x + point1->y * point2->y + point1->z * point2->z);
252 inline float findDistance(XYZ* point1, XYZ* point2)
254 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)));
257 inline float findLength(XYZ* point1)
259 return (fast_sqrt((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z)));
262 inline float findLengthfast(XYZ* point1)
264 return ((point1->x) * (point1->x) + (point1->y) * (point1->y) + (point1->z) * (point1->z));
267 inline float distsq(XYZ* point1, XYZ* point2)
269 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));
272 inline float distsq(XYZ point1, XYZ point2)
274 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));
277 inline float distsqflat(XYZ* point1, XYZ* point2)
279 return ((point1->x - point2->x) * (point1->x - point2->x) + (point1->z - point2->z) * (point1->z - point2->z));
282 inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang)
299 newpoint.z = thePoint.z * cosf(yang) - thePoint.x * sinf(yang);
300 newpoint.x = thePoint.z * sinf(yang) + thePoint.x * cosf(yang);
301 thePoint.z = newpoint.z;
302 thePoint.x = newpoint.x;
306 newpoint.x = thePoint.x * cosf(zang) - thePoint.y * sinf(zang);
307 newpoint.y = thePoint.y * cosf(zang) + thePoint.x * sinf(zang);
308 thePoint.x = newpoint.x;
309 thePoint.y = newpoint.y;
313 newpoint.y = thePoint.y * cosf(xang) - thePoint.z * sinf(xang);
314 newpoint.z = thePoint.y * sinf(xang) + thePoint.z * cosf(xang);
315 thePoint.z = newpoint.z;
316 thePoint.y = newpoint.y;
322 inline float square(float f)
327 inline bool sphere_line_intersection(
328 float x1, float y1, float z1,
329 float x2, float y2, float z2,
330 float x3, float y3, float z3, float r)
333 // x1,y1,z1 P1 coordinates (point of line)
334 // x2,y2,z2 P2 coordinates (point of line)
335 // x3,y3,z3, r P3 coordinates and radius (sphere)
336 // x,y,z intersection coordinates
338 // This function returns a pointer array which first index indicates
339 // the number of intersection point, followed by coordinate pairs.
341 //~ static float x , y , z;
342 static float a, b, c, /*mu,*/ i;
344 if (x1 > x3 + r && x2 > x3 + r)
346 if (x1 < x3 - r && x2 < x3 - r)
348 if (y1 > y3 + r && y2 > y3 + r)
350 if (y1 < y3 - r && y2 < y3 - r)
352 if (z1 > z3 + r && z2 > z3 + r)
354 if (z1 < z3 - r && z2 < z3 - r)
356 a = square(x2 - x1) + square(y2 - y1) + square(z2 - z1);
357 b = 2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3) + (z2 - z1) * (z1 - z3));
358 c = square(x3) + square(y3) +
359 square(z3) + square(x1) +
360 square(y1) + square(z1) -
361 2 * (x3 * x1 + y3 * y1 + z3 * z1) - square(r);
362 i = b * b - 4 * a * c;
371 inline bool sphere_line_intersection(
372 XYZ* p1, XYZ* p2, XYZ* p3, float* r)
375 // x1,p1->y,p1->z P1 coordinates (point of line)
376 // p2->x,p2->y,p2->z P2 coordinates (point of line)
377 // p3->x,p3->y,p3->z, r P3 coordinates and radius (sphere)
378 // x,y,z intersection coordinates
380 // This function returns a pointer array which first index indicates
381 // the number of intersection point, followed by coordinate pairs.
383 //~ static float x , y , z;
384 static float a, b, c, /*mu,*/ i;
386 if (p1->x > p3->x + *r && p2->x > p3->x + *r)
388 if (p1->x < p3->x - *r && p2->x < p3->x - *r)
390 if (p1->y > p3->y + *r && p2->y > p3->y + *r)
392 if (p1->y < p3->y - *r && p2->y < p3->y - *r)
394 if (p1->z > p3->z + *r && p2->z > p3->z + *r)
396 if (p1->z < p3->z - *r && p2->z < p3->z - *r)
398 a = square(p2->x - p1->x) + square(p2->y - p1->y) + square(p2->z - p1->z);
399 b = 2 * ((p2->x - p1->x) * (p1->x - p3->x) + (p2->y - p1->y) * (p1->y - p3->y) + (p2->z - p1->z) * (p1->z - p3->z));
400 c = square(p3->x) + square(p3->y) +
401 square(p3->z) + square(p1->x) +
402 square(p1->y) + square(p1->z) -
403 2 * (p3->x * p1->x + p3->y * p1->y + p3->z * p1->z) - square(*r);
404 i = b * b - 4 * a * c;
413 inline XYZ DoRotationRadian(XYZ thePoint, float xang, float yang, float zang)
421 newpoint.z = oldpoint.z * cosf(yang) - oldpoint.x * sinf(yang);
422 newpoint.x = oldpoint.z * sinf(yang) + oldpoint.x * cosf(yang);
423 oldpoint.z = newpoint.z;
424 oldpoint.x = newpoint.x;
428 newpoint.x = oldpoint.x * cosf(zang) - oldpoint.y * sinf(zang);
429 newpoint.y = oldpoint.y * cosf(zang) + oldpoint.x * sinf(zang);
430 oldpoint.x = newpoint.x;
431 oldpoint.y = newpoint.y;
435 newpoint.y = oldpoint.y * cosf(xang) - oldpoint.z * sinf(xang);
436 newpoint.z = oldpoint.y * sinf(xang) + oldpoint.z * cosf(xang);
437 oldpoint.z = newpoint.z;
438 oldpoint.y = newpoint.y;
444 inline bool DistancePointLine(XYZ* Point, XYZ* LineStart, XYZ* LineEnd, float* Distance, XYZ* Intersection)
449 LineMag = findDistance(LineEnd, LineStart);
451 U = (((Point->x - LineStart->x) * (LineEnd->x - LineStart->x)) +
452 ((Point->y - LineStart->y) * (LineEnd->y - LineStart->y)) +
453 ((Point->z - LineStart->z) * (LineEnd->z - LineStart->z))) /
456 if (U < 0.0f || U > 1.0f)
457 return 0; // closest point does not fall within the line segment
459 Intersection->x = LineStart->x + U * (LineEnd->x - LineStart->x);
460 Intersection->y = LineStart->y + U * (LineEnd->y - LineStart->y);
461 Intersection->z = LineStart->z + U * (LineEnd->z - LineStart->z);
463 *Distance = findDistance(Point, Intersection);