fordon_greeman / moosicpalyer

Branch: 9c07b8b
Switch branches/tags
main
theidioticdev init 9c07b8b 14d ago
3212 lines · 92 KB Raw Blame History
1 /**********************************************************************************************
2 *
3 * raymath v2.1 - Math functions to work with Vector2, Vector3, Vector4, Matrix and Quaternions
4 *
5 * CONVENTIONS:
6 * - Matrix structure is defined as row-major (memory layout) but parameters naming AND all
7 * math operations performed by the library consider the structure as it was column-major
8 * It is like transposed versions of the matrices are used for all the maths
9 * It benefits some functions making them cache-friendly and also avoids matrix
10 * transpositions sometimes required by OpenGL
11 * Example: In memory order, row0 is [m0 m4 m8 m12] but in semantic math row0 is [m0 m1 m2 m3]
12 * - All math is float only, no doubles usage
13 * - Functions are always self-contained, no function use another raymath function inside,
14 * required code is directly re-implemented inside
15 * - Functions input parameters are always received by value (2 unavoidable exceptions)
16 * - Functions use always a "result" variable for return (except C++ operators)
17 * - Functions are always defined inline
18 * - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience)
19 * - No compound literals used to make sure the library is compatible with C++
20 *
21 * CONFIGURATION:
22 * #define RAYMATH_IMPLEMENTATION
23 * Generates the implementation of the library into the included file
24 * If not defined, the library is in header only mode and can be included in other headers
25 * or source files without problems. But only ONE file should hold the implementation
26 *
27 * #define RAYMATH_STATIC_INLINE
28 * Define static inline functions code, so #include header suffices for use
29 * This may use up lots of memory
30 *
31 * #define RAYMATH_DISABLE_CPP_OPERATORS
32 * Disables C++ operator overloads for raymath types.
33 *
34 * #define RAYMATH_USE_SIMD_INTRINSICS 1
35 * Try to enable SIMD intrinsics for MatrixMultiply()
36 * Note that users enabling it must be aware of the target platform where application will
37 * run to support the selected SIMD intrinsic, for now, only SSE is supported
38 *
39 * LICENSE: zlib/libpng
40 *
41 * Copyright (c) 2015-2026 Ramon Santamaria (@raysan5)
42 *
43 * This software is provided "as-is", without any express or implied warranty. In no event
44 * will the authors be held liable for any damages arising from the use of this software.
45 *
46 * Permission is granted to anyone to use this software for any purpose, including commercial
47 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
48 *
49 * 1. The origin of this software must not be misrepresented; you must not claim that you
50 * wrote the original software. If you use this software in a product, an acknowledgment
51 * in the product documentation would be appreciated but is not required.
52 *
53 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
54 * as being the original software.
55 *
56 * 3. This notice may not be removed or altered from any source distribution.
57 *
58 **********************************************************************************************/
59
60 #ifndef RAYMATH_H
61 #define RAYMATH_H
62
63 #if defined(RAYMATH_IMPLEMENTATION) && defined(RAYMATH_STATIC_INLINE)
64 #error "Specifying both RAYMATH_IMPLEMENTATION and RAYMATH_STATIC_INLINE is contradictory"
65 #endif
66
67 // Function specifiers definition
68 #if defined(RAYMATH_IMPLEMENTATION)
69 #if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED)
70 #define RMAPI __declspec(dllexport) extern inline // Building raylib as a Win32 shared library (.dll)
71 #elif defined(BUILD_LIBTYPE_SHARED)
72 #define RMAPI __attribute__((visibility("default"))) // Building raylib as a Unix shared library (.so/.dylib)
73 #elif defined(_WIN32) && defined(USE_LIBTYPE_SHARED)
74 #define RMAPI __declspec(dllimport) // Using raylib as a Win32 shared library (.dll)
75 #else
76 #define RMAPI extern inline // Provide external definition
77 #endif
78 #elif defined(RAYMATH_STATIC_INLINE)
79 #define RMAPI static inline // Functions may be inlined, no external out-of-line definition
80 #else
81 #if defined(__TINYC__)
82 #define RMAPI static inline // plain inline not supported by tinycc (See issue #435)
83 #else
84 #define RMAPI inline // Functions may be inlined or external definition used
85 #endif
86 #endif
87
88 //----------------------------------------------------------------------------------
89 // Defines and Macros
90 //----------------------------------------------------------------------------------
91 #ifndef PI
92 #define PI 3.14159265358979323846f
93 #endif
94
95 #ifndef EPSILON
96 #define EPSILON 0.000001f
97 #endif
98
99 #ifndef DEG2RAD
100 #define DEG2RAD (PI/180.0f)
101 #endif
102
103 #ifndef RAD2DEG
104 #define RAD2DEG (180.0f/PI)
105 #endif
106
107 // Get float vector for Matrix
108 #ifndef MatrixToFloat
109 #define MatrixToFloat(mat) (MatrixToFloatV(mat).v)
110 #endif
111
112 // Get float vector for Vector3
113 #ifndef Vector3ToFloat
114 #define Vector3ToFloat(vec) (Vector3ToFloatV(vec).v)
115 #endif
116
117 //----------------------------------------------------------------------------------
118 // Types and Structures Definition
119 //----------------------------------------------------------------------------------
120 #if !defined(RL_VECTOR2_TYPE)
121 // Vector2 type
122 typedef struct Vector2 {
123 float x;
124 float y;
125 } Vector2;
126 #define RL_VECTOR2_TYPE
127 #endif
128
129 #if !defined(RL_VECTOR3_TYPE)
130 // Vector3 type
131 typedef struct Vector3 {
132 float x;
133 float y;
134 float z;
135 } Vector3;
136 #define RL_VECTOR3_TYPE
137 #endif
138
139 #if !defined(RL_VECTOR4_TYPE)
140 // Vector4 type
141 typedef struct Vector4 {
142 float x;
143 float y;
144 float z;
145 float w;
146 } Vector4;
147 #define RL_VECTOR4_TYPE
148 #endif
149
150 #if !defined(RL_QUATERNION_TYPE)
151 // Quaternion type
152 typedef Vector4 Quaternion;
153 #define RL_QUATERNION_TYPE
154 #endif
155
156 #if !defined(RL_MATRIX_TYPE)
157 // Matrix type (OpenGL style 4x4 - right handed, column major)
158 typedef struct Matrix {
159 float m0, m4, m8, m12; // Matrix first row (4 components)
160 float m1, m5, m9, m13; // Matrix second row (4 components)
161 float m2, m6, m10, m14; // Matrix third row (4 components)
162 float m3, m7, m11, m15; // Matrix fourth row (4 components)
163 } Matrix;
164 #define RL_MATRIX_TYPE
165 #endif
166
167 // NOTE: Helper types to be used instead of array return types for *ToFloat functions
168 #if !defined(RL_FLOAT3_TYPE)
169 typedef struct float3 {
170 float v[3];
171 } float3;
172 #define RL_FLOAT3_TYPE
173 #endif
174
175 #if !defined(RL_FLOAT16_TYPE)
176 typedef struct float16 {
177 float v[16];
178 } float16;
179 #define RL_FLOAT16_TYPE
180 #endif
181
182 #include <math.h> // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabsf()
183
184 #if RAYMATH_USE_SIMD_INTRINSICS
185 // SIMD is used on the most costly raymath function MatrixMultiply()
186 // NOTE: Only SSE intrinsics support implemented
187 // TODO: Consider support for other SIMD intrinsics:
188 // - SSEx, AVX, AVX2, FMA, NEON, RVV
189 /*
190 #if defined(__SSE4_2__)
191 #include <nmmintrin.h>
192 #define RAYMATH_SSE42_ENABLED
193 #elif defined(__SSE4_1__)
194 #include <smmintrin.h>
195 #define RAYMATH_SSE41_ENABLED
196 #elif defined(__SSSE3__)
197 #include <tmmintrin.h>
198 #define RAYMATH_SSSE3_ENABLED
199 #elif defined(__SSE3__)
200 #include <pmmintrin.h>
201 #define RAYMATH_SSE3_ENABLED
202 #elif defined(__SSE2__) || (defined(_M_AMD64) || defined(_M_X64)) // SSE2 x64
203 #include <emmintrin.h>
204 #define RAYMATH_SSE2_ENABLED
205 #endif
206 */
207 #if defined(__SSE__) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP >= 1))
208 #include <xmmintrin.h>
209 #define RAYMATH_SSE_ENABLED
210 #endif
211 #endif
212
213 //----------------------------------------------------------------------------------
214 // Module Functions Definition - Utils math
215 //----------------------------------------------------------------------------------
216
217 // Clamp float value
218 RMAPI float Clamp(float value, float min, float max)
219 {
220 float result = (value < min)? min : value;
221
222 if (result > max) result = max;
223
224 return result;
225 }
226
227 // Calculate linear interpolation between two floats
228 RMAPI float Lerp(float start, float end, float amount)
229 {
230 float result = start + amount*(end - start);
231
232 return result;
233 }
234
235 // Normalize input value within input range
236 RMAPI float Normalize(float value, float start, float end)
237 {
238 float result = (value - start)/(end - start);
239
240 return result;
241 }
242
243 // Remap input value within input range to output range
244 RMAPI float Remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd)
245 {
246 float result = (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;
247
248 return result;
249 }
250
251 // Wrap input value from min to max
252 RMAPI float Wrap(float value, float min, float max)
253 {
254 float result = value - (max - min)*floorf((value - min)/(max - min));
255
256 return result;
257 }
258
259 // Check whether two given floats are almost equal
260 RMAPI int FloatEquals(float x, float y)
261 {
262 #if !defined(EPSILON)
263 #define EPSILON 0.000001f
264 #endif
265
266 int result = (fabsf(x - y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))));
267
268 return result;
269 }
270
271 //----------------------------------------------------------------------------------
272 // Module Functions Definition - Vector2 math
273 //----------------------------------------------------------------------------------
274
275 // Vector with components value 0.0f
276 RMAPI Vector2 Vector2Zero(void)
277 {
278 Vector2 result = { 0.0f, 0.0f };
279
280 return result;
281 }
282
283 // Vector with components value 1.0f
284 RMAPI Vector2 Vector2One(void)
285 {
286 Vector2 result = { 1.0f, 1.0f };
287
288 return result;
289 }
290
291 // Add two vectors (v1 + v2)
292 RMAPI Vector2 Vector2Add(Vector2 v1, Vector2 v2)
293 {
294 Vector2 result = { v1.x + v2.x, v1.y + v2.y };
295
296 return result;
297 }
298
299 // Add vector and float value
300 RMAPI Vector2 Vector2AddValue(Vector2 v, float add)
301 {
302 Vector2 result = { v.x + add, v.y + add };
303
304 return result;
305 }
306
307 // Subtract two vectors (v1 - v2)
308 RMAPI Vector2 Vector2Subtract(Vector2 v1, Vector2 v2)
309 {
310 Vector2 result = { v1.x - v2.x, v1.y - v2.y };
311
312 return result;
313 }
314
315 // Subtract vector by float value
316 RMAPI Vector2 Vector2SubtractValue(Vector2 v, float sub)
317 {
318 Vector2 result = { v.x - sub, v.y - sub };
319
320 return result;
321 }
322
323 // Calculate vector length
324 RMAPI float Vector2Length(Vector2 v)
325 {
326 float result = sqrtf((v.x*v.x) + (v.y*v.y));
327
328 return result;
329 }
330
331 // Calculate vector square length
332 RMAPI float Vector2LengthSqr(Vector2 v)
333 {
334 float result = (v.x*v.x) + (v.y*v.y);
335
336 return result;
337 }
338
339 // Calculate two vectors dot product
340 RMAPI float Vector2DotProduct(Vector2 v1, Vector2 v2)
341 {
342 float result = (v1.x*v2.x + v1.y*v2.y);
343
344 return result;
345 }
346
347 // Calculate two vectors cross product
348 RMAPI float Vector2CrossProduct(Vector2 v1, Vector2 v2)
349 {
350 float result = (v1.x*v2.y - v1.y*v2.x);
351
352 return result;
353 }
354
355 // Calculate distance between two vectors
356 RMAPI float Vector2Distance(Vector2 v1, Vector2 v2)
357 {
358 float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
359
360 return result;
361 }
362
363 // Calculate square distance between two vectors
364 RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2)
365 {
366 float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
367
368 return result;
369 }
370
371 // Calculate the signed angle from v1 to v2, relative to the origin (0, 0)
372 // NOTE: Coordinate system convention: positive X right, positive Y down
373 // positive angles appear clockwise, and negative angles appear counterclockwise
374 RMAPI float Vector2Angle(Vector2 v1, Vector2 v2)
375 {
376 float result = 0.0f;
377
378 float dot = v1.x*v2.x + v1.y*v2.y;
379 float det = v1.x*v2.y - v1.y*v2.x;
380
381 result = atan2f(det, dot);
382
383 return result;
384 }
385
386 // Calculate angle defined by a two vectors line
387 // NOTE: Parameters need to be normalized
388 // Current implementation should be aligned with glm::angle
389 RMAPI float Vector2LineAngle(Vector2 start, Vector2 end)
390 {
391 float result = 0.0f;
392
393 result = -atan2f(end.y - start.y, end.x - start.x);
394
395 return result;
396 }
397
398 // Scale vector (multiply by value)
399 RMAPI Vector2 Vector2Scale(Vector2 v, float scale)
400 {
401 Vector2 result = { v.x*scale, v.y*scale };
402
403 return result;
404 }
405
406 // Multiply vector by vector
407 RMAPI Vector2 Vector2Multiply(Vector2 v1, Vector2 v2)
408 {
409 Vector2 result = { v1.x*v2.x, v1.y*v2.y };
410
411 return result;
412 }
413
414 // Negate vector
415 RMAPI Vector2 Vector2Negate(Vector2 v)
416 {
417 Vector2 result = { -v.x, -v.y };
418
419 return result;
420 }
421
422 // Divide vector by vector
423 RMAPI Vector2 Vector2Divide(Vector2 v1, Vector2 v2)
424 {
425 Vector2 result = { v1.x/v2.x, v1.y/v2.y };
426
427 return result;
428 }
429
430 // Normalize provided vector
431 RMAPI Vector2 Vector2Normalize(Vector2 v)
432 {
433 Vector2 result = { 0 };
434 float length = sqrtf((v.x*v.x) + (v.y*v.y));
435
436 if (length > 0)
437 {
438 float ilength = 1.0f/length;
439 result.x = v.x*ilength;
440 result.y = v.y*ilength;
441 }
442
443 return result;
444 }
445
446 // Transforms a Vector2 by a given Matrix
447 RMAPI Vector2 Vector2Transform(Vector2 v, Matrix mat)
448 {
449 Vector2 result = { 0 };
450
451 float x = v.x;
452 float y = v.y;
453 float z = 0;
454
455 result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
456 result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
457
458 return result;
459 }
460
461 // Calculate linear interpolation between two vectors
462 RMAPI Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount)
463 {
464 Vector2 result = { 0 };
465
466 result.x = v1.x + amount*(v2.x - v1.x);
467 result.y = v1.y + amount*(v2.y - v1.y);
468
469 return result;
470 }
471
472 // Calculate reflected vector to normal
473 RMAPI Vector2 Vector2Reflect(Vector2 v, Vector2 normal)
474 {
475 Vector2 result = { 0 };
476
477 float dotProduct = (v.x*normal.x + v.y*normal.y); // Dot product
478
479 result.x = v.x - (2.0f*normal.x)*dotProduct;
480 result.y = v.y - (2.0f*normal.y)*dotProduct;
481
482 return result;
483 }
484
485 // Get min value for each pair of components
486 RMAPI Vector2 Vector2Min(Vector2 v1, Vector2 v2)
487 {
488 Vector2 result = { 0 };
489
490 result.x = fminf(v1.x, v2.x);
491 result.y = fminf(v1.y, v2.y);
492
493 return result;
494 }
495
496 // Get max value for each pair of components
497 RMAPI Vector2 Vector2Max(Vector2 v1, Vector2 v2)
498 {
499 Vector2 result = { 0 };
500
501 result.x = fmaxf(v1.x, v2.x);
502 result.y = fmaxf(v1.y, v2.y);
503
504 return result;
505 }
506
507 // Rotate vector by angle
508 RMAPI Vector2 Vector2Rotate(Vector2 v, float angle)
509 {
510 Vector2 result = { 0 };
511
512 float cosres = cosf(angle);
513 float sinres = sinf(angle);
514
515 result.x = v.x*cosres - v.y*sinres;
516 result.y = v.x*sinres + v.y*cosres;
517
518 return result;
519 }
520
521 // Move Vector towards target
522 RMAPI Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance)
523 {
524 Vector2 result = { 0 };
525
526 float dx = target.x - v.x;
527 float dy = target.y - v.y;
528 float value = (dx*dx) + (dy*dy);
529
530 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;
531
532 float dist = sqrtf(value);
533
534 result.x = v.x + dx/dist*maxDistance;
535 result.y = v.y + dy/dist*maxDistance;
536
537 return result;
538 }
539
540 // Invert the given vector
541 RMAPI Vector2 Vector2Invert(Vector2 v)
542 {
543 Vector2 result = { 1.0f/v.x, 1.0f/v.y };
544
545 return result;
546 }
547
548 // Clamp the components of the vector between
549 // min and max values specified by the given vectors
550 RMAPI Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max)
551 {
552 Vector2 result = { 0 };
553
554 result.x = fminf(max.x, fmaxf(min.x, v.x));
555 result.y = fminf(max.y, fmaxf(min.y, v.y));
556
557 return result;
558 }
559
560 // Clamp the magnitude of the vector between two min and max values
561 RMAPI Vector2 Vector2ClampValue(Vector2 v, float min, float max)
562 {
563 Vector2 result = v;
564
565 float length = (v.x*v.x) + (v.y*v.y);
566 if (length > 0.0f)
567 {
568 length = sqrtf(length);
569
570 float scale = 1; // By default, 1 as the neutral element
571 if (length < min) scale = min/length;
572 else if (length > max) scale = max/length;
573
574 result.x = v.x*scale;
575 result.y = v.y*scale;
576 }
577
578 return result;
579 }
580
581 // Check whether two given vectors are almost equal
582 RMAPI int Vector2Equals(Vector2 p, Vector2 q)
583 {
584 #if !defined(EPSILON)
585 #define EPSILON 0.000001f
586 #endif
587
588 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
589 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))));
590
591 return result;
592 }
593
594 // Compute the direction of a refracted ray
595 // v: normalized direction of the incoming ray
596 // n: normalized normal vector of the interface of two optical media
597 // r: ratio of the refractive index of the medium from where the ray comes
598 // to the refractive index of the medium on the other side of the surface
599 RMAPI Vector2 Vector2Refract(Vector2 v, Vector2 n, float r)
600 {
601 Vector2 result = { 0 };
602
603 float dot = v.x*n.x + v.y*n.y;
604 float d = 1.0f - r*r*(1.0f - dot*dot);
605
606 if (d >= 0.0f)
607 {
608 d = sqrtf(d);
609 v.x = r*v.x - (r*dot + d)*n.x;
610 v.y = r*v.y - (r*dot + d)*n.y;
611
612 result = v;
613 }
614
615 return result;
616 }
617
618
619 //----------------------------------------------------------------------------------
620 // Module Functions Definition - Vector3 math
621 //----------------------------------------------------------------------------------
622
623 // Vector with components value 0.0f
624 RMAPI Vector3 Vector3Zero(void)
625 {
626 Vector3 result = { 0.0f, 0.0f, 0.0f };
627
628 return result;
629 }
630
631 // Vector with components value 1.0f
632 RMAPI Vector3 Vector3One(void)
633 {
634 Vector3 result = { 1.0f, 1.0f, 1.0f };
635
636 return result;
637 }
638
639 // Add two vectors
640 RMAPI Vector3 Vector3Add(Vector3 v1, Vector3 v2)
641 {
642 Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
643
644 return result;
645 }
646
647 // Add vector and float value
648 RMAPI Vector3 Vector3AddValue(Vector3 v, float add)
649 {
650 Vector3 result = { v.x + add, v.y + add, v.z + add };
651
652 return result;
653 }
654
655 // Subtract two vectors
656 RMAPI Vector3 Vector3Subtract(Vector3 v1, Vector3 v2)
657 {
658 Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
659
660 return result;
661 }
662
663 // Subtract vector by float value
664 RMAPI Vector3 Vector3SubtractValue(Vector3 v, float sub)
665 {
666 Vector3 result = { v.x - sub, v.y - sub, v.z - sub };
667
668 return result;
669 }
670
671 // Multiply vector by scalar
672 RMAPI Vector3 Vector3Scale(Vector3 v, float scalar)
673 {
674 Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };
675
676 return result;
677 }
678
679 // Multiply vector by vector
680 RMAPI Vector3 Vector3Multiply(Vector3 v1, Vector3 v2)
681 {
682 Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };
683
684 return result;
685 }
686
687 // Calculate two vectors cross product
688 RMAPI Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2)
689 {
690 Vector3 result = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
691
692 return result;
693 }
694
695 // Calculate one vector perpendicular vector
696 RMAPI Vector3 Vector3Perpendicular(Vector3 v)
697 {
698 Vector3 result = { 0 };
699
700 float min = fabsf(v.x);
701 Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
702
703 if (fabsf(v.y) < min)
704 {
705 min = fabsf(v.y);
706 Vector3 tmp = {0.0f, 1.0f, 0.0f};
707 cardinalAxis = tmp;
708 }
709
710 if (fabsf(v.z) < min)
711 {
712 Vector3 tmp = {0.0f, 0.0f, 1.0f};
713 cardinalAxis = tmp;
714 }
715
716 // Cross product between vectors
717 result.x = v.y*cardinalAxis.z - v.z*cardinalAxis.y;
718 result.y = v.z*cardinalAxis.x - v.x*cardinalAxis.z;
719 result.z = v.x*cardinalAxis.y - v.y*cardinalAxis.x;
720
721 return result;
722 }
723
724 // Calculate vector length
725 RMAPI float Vector3Length(const Vector3 v)
726 {
727 float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
728
729 return result;
730 }
731
732 // Calculate vector square length
733 RMAPI float Vector3LengthSqr(const Vector3 v)
734 {
735 float result = v.x*v.x + v.y*v.y + v.z*v.z;
736
737 return result;
738 }
739
740 // Calculate two vectors dot product
741 RMAPI float Vector3DotProduct(Vector3 v1, Vector3 v2)
742 {
743 float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
744
745 return result;
746 }
747
748 // Calculate distance between two vectors
749 RMAPI float Vector3Distance(Vector3 v1, Vector3 v2)
750 {
751 float result = 0.0f;
752
753 float dx = v2.x - v1.x;
754 float dy = v2.y - v1.y;
755 float dz = v2.z - v1.z;
756 result = sqrtf(dx*dx + dy*dy + dz*dz);
757
758 return result;
759 }
760
761 // Calculate square distance between two vectors
762 RMAPI float Vector3DistanceSqr(Vector3 v1, Vector3 v2)
763 {
764 float result = 0.0f;
765
766 float dx = v2.x - v1.x;
767 float dy = v2.y - v1.y;
768 float dz = v2.z - v1.z;
769 result = dx*dx + dy*dy + dz*dz;
770
771 return result;
772 }
773
774 // Calculate angle between two vectors
775 RMAPI float Vector3Angle(Vector3 v1, Vector3 v2)
776 {
777 float result = 0.0f;
778
779 Vector3 cross = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
780 float len = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z);
781 float dot = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
782 result = atan2f(len, dot);
783
784 return result;
785 }
786
787 // Negate provided vector (invert direction)
788 RMAPI Vector3 Vector3Negate(Vector3 v)
789 {
790 Vector3 result = { -v.x, -v.y, -v.z };
791
792 return result;
793 }
794
795 // Divide vector by vector
796 RMAPI Vector3 Vector3Divide(Vector3 v1, Vector3 v2)
797 {
798 Vector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };
799
800 return result;
801 }
802
803 // Normalize provided vector
804 RMAPI Vector3 Vector3Normalize(Vector3 v)
805 {
806 Vector3 result = v;
807
808 float length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
809 if (length != 0.0f)
810 {
811 float ilength = 1.0f/length;
812
813 result.x *= ilength;
814 result.y *= ilength;
815 result.z *= ilength;
816 }
817
818 return result;
819 }
820
821 //Calculate the projection of the vector v1 on to v2
822 RMAPI Vector3 Vector3Project(Vector3 v1, Vector3 v2)
823 {
824 Vector3 result = { 0 };
825
826 float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
827 float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
828
829 float mag = v1dv2/v2dv2;
830
831 result.x = v2.x*mag;
832 result.y = v2.y*mag;
833 result.z = v2.z*mag;
834
835 return result;
836 }
837
838 //Calculate the rejection of the vector v1 on to v2
839 RMAPI Vector3 Vector3Reject(Vector3 v1, Vector3 v2)
840 {
841 Vector3 result = { 0 };
842
843 float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
844 float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
845
846 float mag = v1dv2/v2dv2;
847
848 result.x = v1.x - (v2.x*mag);
849 result.y = v1.y - (v2.y*mag);
850 result.z = v1.z - (v2.z*mag);
851
852 return result;
853 }
854
855 // Orthonormalize provided vectors
856 // Makes vectors normalized and orthogonal to each other
857 // Gram-Schmidt function implementation
858 RMAPI void Vector3OrthoNormalize(Vector3 *v1, Vector3 *v2)
859 {
860 float length = 0.0f;
861 float ilength = 0.0f;
862
863 // Vector3Normalize(*v1);
864 Vector3 v = *v1;
865 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
866 if (length == 0.0f) length = 1.0f;
867 ilength = 1.0f/length;
868 v1->x *= ilength;
869 v1->y *= ilength;
870 v1->z *= ilength;
871
872 // Vector3CrossProduct(*v1, *v2)
873 Vector3 vn1 = { v1->y*v2->z - v1->z*v2->y, v1->z*v2->x - v1->x*v2->z, v1->x*v2->y - v1->y*v2->x };
874
875 // Vector3Normalize(vn1);
876 v = vn1;
877 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
878 if (length == 0.0f) length = 1.0f;
879 ilength = 1.0f/length;
880 vn1.x *= ilength;
881 vn1.y *= ilength;
882 vn1.z *= ilength;
883
884 // Vector3CrossProduct(vn1, *v1)
885 Vector3 vn2 = { vn1.y*v1->z - vn1.z*v1->y, vn1.z*v1->x - vn1.x*v1->z, vn1.x*v1->y - vn1.y*v1->x };
886
887 *v2 = vn2;
888 }
889
890 // Transforms a Vector3 by a given Matrix
891 RMAPI Vector3 Vector3Transform(Vector3 v, Matrix mat)
892 {
893 Vector3 result = { 0 };
894
895 float x = v.x;
896 float y = v.y;
897 float z = v.z;
898
899 result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
900 result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
901 result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;
902
903 return result;
904 }
905
906 // Transform a vector by quaternion rotation
907 RMAPI Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q)
908 {
909 Vector3 result = { 0 };
910
911 result.x = v.x*(q.x*q.x + q.w*q.w - q.y*q.y - q.z*q.z) + v.y*(2*q.x*q.y - 2*q.w*q.z) + v.z*(2*q.x*q.z + 2*q.w*q.y);
912 result.y = v.x*(2*q.w*q.z + 2*q.x*q.y) + v.y*(q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z) + v.z*(-2*q.w*q.x + 2*q.y*q.z);
913 result.z = v.x*(-2*q.w*q.y + 2*q.x*q.z) + v.y*(2*q.w*q.x + 2*q.y*q.z)+ v.z*(q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z);
914
915 return result;
916 }
917
918 // Rotates a vector around an axis
919 RMAPI Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle)
920 {
921 // Using Euler-Rodrigues Formula
922 // Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
923
924 Vector3 result = v;
925
926 // Vector3Normalize(axis);
927 float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
928 if (length == 0.0f) length = 1.0f;
929 float ilength = 1.0f/length;
930 axis.x *= ilength;
931 axis.y *= ilength;
932 axis.z *= ilength;
933
934 angle /= 2.0f;
935 float a = sinf(angle);
936 float b = axis.x*a;
937 float c = axis.y*a;
938 float d = axis.z*a;
939 a = cosf(angle);
940 Vector3 w = { b, c, d };
941
942 // Vector3CrossProduct(w, v)
943 Vector3 wv = { w.y*v.z - w.z*v.y, w.z*v.x - w.x*v.z, w.x*v.y - w.y*v.x };
944
945 // Vector3CrossProduct(w, wv)
946 Vector3 wwv = { w.y*wv.z - w.z*wv.y, w.z*wv.x - w.x*wv.z, w.x*wv.y - w.y*wv.x };
947
948 // Vector3Scale(wv, 2*a)
949 a *= 2;
950 wv.x *= a;
951 wv.y *= a;
952 wv.z *= a;
953
954 // Vector3Scale(wwv, 2)
955 wwv.x *= 2;
956 wwv.y *= 2;
957 wwv.z *= 2;
958
959 result.x += wv.x;
960 result.y += wv.y;
961 result.z += wv.z;
962
963 result.x += wwv.x;
964 result.y += wwv.y;
965 result.z += wwv.z;
966
967 return result;
968 }
969
970 // Move Vector towards target
971 RMAPI Vector3 Vector3MoveTowards(Vector3 v, Vector3 target, float maxDistance)
972 {
973 Vector3 result = { 0 };
974
975 float dx = target.x - v.x;
976 float dy = target.y - v.y;
977 float dz = target.z - v.z;
978 float value = (dx*dx) + (dy*dy) + (dz*dz);
979
980 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;
981
982 float dist = sqrtf(value);
983
984 result.x = v.x + dx/dist*maxDistance;
985 result.y = v.y + dy/dist*maxDistance;
986 result.z = v.z + dz/dist*maxDistance;
987
988 return result;
989 }
990
991 // Calculate linear interpolation between two vectors
992 RMAPI Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount)
993 {
994 Vector3 result = { 0 };
995
996 result.x = v1.x + amount*(v2.x - v1.x);
997 result.y = v1.y + amount*(v2.y - v1.y);
998 result.z = v1.z + amount*(v2.z - v1.z);
999
1000 return result;
1001 }
1002
1003 // Calculate cubic hermite interpolation between two vectors and their tangents
1004 // as described in the GLTF 2.0 specification: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#interpolation-cubic
1005 RMAPI Vector3 Vector3CubicHermite(Vector3 v1, Vector3 tangent1, Vector3 v2, Vector3 tangent2, float amount)
1006 {
1007 Vector3 result = { 0 };
1008
1009 float amountPow2 = amount*amount;
1010 float amountPow3 = amount*amount*amount;
1011
1012 result.x = (2*amountPow3 - 3*amountPow2 + 1)*v1.x + (amountPow3 - 2*amountPow2 + amount)*tangent1.x + (-2*amountPow3 + 3*amountPow2)*v2.x + (amountPow3 - amountPow2)*tangent2.x;
1013 result.y = (2*amountPow3 - 3*amountPow2 + 1)*v1.y + (amountPow3 - 2*amountPow2 + amount)*tangent1.y + (-2*amountPow3 + 3*amountPow2)*v2.y + (amountPow3 - amountPow2)*tangent2.y;
1014 result.z = (2*amountPow3 - 3*amountPow2 + 1)*v1.z + (amountPow3 - 2*amountPow2 + amount)*tangent1.z + (-2*amountPow3 + 3*amountPow2)*v2.z + (amountPow3 - amountPow2)*tangent2.z;
1015
1016 return result;
1017 }
1018
1019 // Calculate reflected vector to normal
1020 RMAPI Vector3 Vector3Reflect(Vector3 v, Vector3 normal)
1021 {
1022 Vector3 result = { 0 };
1023
1024 // I is the original vector
1025 // N is the normal of the incident plane
1026 // R = I - (2*N*(DotProduct[I, N]))
1027
1028 float dotProduct = (v.x*normal.x + v.y*normal.y + v.z*normal.z);
1029
1030 result.x = v.x - (2.0f*normal.x)*dotProduct;
1031 result.y = v.y - (2.0f*normal.y)*dotProduct;
1032 result.z = v.z - (2.0f*normal.z)*dotProduct;
1033
1034 return result;
1035 }
1036
1037 // Get min value for each pair of components
1038 RMAPI Vector3 Vector3Min(Vector3 v1, Vector3 v2)
1039 {
1040 Vector3 result = { 0 };
1041
1042 result.x = fminf(v1.x, v2.x);
1043 result.y = fminf(v1.y, v2.y);
1044 result.z = fminf(v1.z, v2.z);
1045
1046 return result;
1047 }
1048
1049 // Get max value for each pair of components
1050 RMAPI Vector3 Vector3Max(Vector3 v1, Vector3 v2)
1051 {
1052 Vector3 result = { 0 };
1053
1054 result.x = fmaxf(v1.x, v2.x);
1055 result.y = fmaxf(v1.y, v2.y);
1056 result.z = fmaxf(v1.z, v2.z);
1057
1058 return result;
1059 }
1060
1061 // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
1062 // NOTE: Assumes P is on the plane of the triangle
1063 RMAPI Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c)
1064 {
1065 Vector3 result = { 0 };
1066
1067 Vector3 v0 = { b.x - a.x, b.y - a.y, b.z - a.z }; // Vector3Subtract(b, a)
1068 Vector3 v1 = { c.x - a.x, c.y - a.y, c.z - a.z }; // Vector3Subtract(c, a)
1069 Vector3 v2 = { p.x - a.x, p.y - a.y, p.z - a.z }; // Vector3Subtract(p, a)
1070 float d00 = (v0.x*v0.x + v0.y*v0.y + v0.z*v0.z); // Vector3DotProduct(v0, v0)
1071 float d01 = (v0.x*v1.x + v0.y*v1.y + v0.z*v1.z); // Vector3DotProduct(v0, v1)
1072 float d11 = (v1.x*v1.x + v1.y*v1.y + v1.z*v1.z); // Vector3DotProduct(v1, v1)
1073 float d20 = (v2.x*v0.x + v2.y*v0.y + v2.z*v0.z); // Vector3DotProduct(v2, v0)
1074 float d21 = (v2.x*v1.x + v2.y*v1.y + v2.z*v1.z); // Vector3DotProduct(v2, v1)
1075
1076 float denom = d00*d11 - d01*d01;
1077
1078 result.y = (d11*d20 - d01*d21)/denom;
1079 result.z = (d00*d21 - d01*d20)/denom;
1080 result.x = 1.0f - (result.z + result.y);
1081
1082 return result;
1083 }
1084
1085 // Projects a Vector3 from screen space into object space
1086 // NOTE: Self-contained function, no other raymath functions are called
1087 RMAPI Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view)
1088 {
1089 Vector3 result = { 0 };
1090
1091 // Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it
1092 Matrix matViewProj = { // MatrixMultiply(view, projection);
1093 view.m0*projection.m0 + view.m1*projection.m4 + view.m2*projection.m8 + view.m3*projection.m12,
1094 view.m0*projection.m1 + view.m1*projection.m5 + view.m2*projection.m9 + view.m3*projection.m13,
1095 view.m0*projection.m2 + view.m1*projection.m6 + view.m2*projection.m10 + view.m3*projection.m14,
1096 view.m0*projection.m3 + view.m1*projection.m7 + view.m2*projection.m11 + view.m3*projection.m15,
1097 view.m4*projection.m0 + view.m5*projection.m4 + view.m6*projection.m8 + view.m7*projection.m12,
1098 view.m4*projection.m1 + view.m5*projection.m5 + view.m6*projection.m9 + view.m7*projection.m13,
1099 view.m4*projection.m2 + view.m5*projection.m6 + view.m6*projection.m10 + view.m7*projection.m14,
1100 view.m4*projection.m3 + view.m5*projection.m7 + view.m6*projection.m11 + view.m7*projection.m15,
1101 view.m8*projection.m0 + view.m9*projection.m4 + view.m10*projection.m8 + view.m11*projection.m12,
1102 view.m8*projection.m1 + view.m9*projection.m5 + view.m10*projection.m9 + view.m11*projection.m13,
1103 view.m8*projection.m2 + view.m9*projection.m6 + view.m10*projection.m10 + view.m11*projection.m14,
1104 view.m8*projection.m3 + view.m9*projection.m7 + view.m10*projection.m11 + view.m11*projection.m15,
1105 view.m12*projection.m0 + view.m13*projection.m4 + view.m14*projection.m8 + view.m15*projection.m12,
1106 view.m12*projection.m1 + view.m13*projection.m5 + view.m14*projection.m9 + view.m15*projection.m13,
1107 view.m12*projection.m2 + view.m13*projection.m6 + view.m14*projection.m10 + view.m15*projection.m14,
1108 view.m12*projection.m3 + view.m13*projection.m7 + view.m14*projection.m11 + view.m15*projection.m15 };
1109
1110 // Calculate inverted matrix -> MatrixInvert(matViewProj);
1111 // Cache the matrix values (speed optimization)
1112 float a00 = matViewProj.m0, a01 = matViewProj.m1, a02 = matViewProj.m2, a03 = matViewProj.m3;
1113 float a10 = matViewProj.m4, a11 = matViewProj.m5, a12 = matViewProj.m6, a13 = matViewProj.m7;
1114 float a20 = matViewProj.m8, a21 = matViewProj.m9, a22 = matViewProj.m10, a23 = matViewProj.m11;
1115 float a30 = matViewProj.m12, a31 = matViewProj.m13, a32 = matViewProj.m14, a33 = matViewProj.m15;
1116
1117 float b00 = a00*a11 - a01*a10;
1118 float b01 = a00*a12 - a02*a10;
1119 float b02 = a00*a13 - a03*a10;
1120 float b03 = a01*a12 - a02*a11;
1121 float b04 = a01*a13 - a03*a11;
1122 float b05 = a02*a13 - a03*a12;
1123 float b06 = a20*a31 - a21*a30;
1124 float b07 = a20*a32 - a22*a30;
1125 float b08 = a20*a33 - a23*a30;
1126 float b09 = a21*a32 - a22*a31;
1127 float b10 = a21*a33 - a23*a31;
1128 float b11 = a22*a33 - a23*a32;
1129
1130 // Calculate the invert determinant (inlined to avoid double-caching)
1131 float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
1132
1133 Matrix matViewProjInv = {
1134 (a11*b11 - a12*b10 + a13*b09)*invDet,
1135 (-a01*b11 + a02*b10 - a03*b09)*invDet,
1136 (a31*b05 - a32*b04 + a33*b03)*invDet,
1137 (-a21*b05 + a22*b04 - a23*b03)*invDet,
1138 (-a10*b11 + a12*b08 - a13*b07)*invDet,
1139 (a00*b11 - a02*b08 + a03*b07)*invDet,
1140 (-a30*b05 + a32*b02 - a33*b01)*invDet,
1141 (a20*b05 - a22*b02 + a23*b01)*invDet,
1142 (a10*b10 - a11*b08 + a13*b06)*invDet,
1143 (-a00*b10 + a01*b08 - a03*b06)*invDet,
1144 (a30*b04 - a31*b02 + a33*b00)*invDet,
1145 (-a20*b04 + a21*b02 - a23*b00)*invDet,
1146 (-a10*b09 + a11*b07 - a12*b06)*invDet,
1147 (a00*b09 - a01*b07 + a02*b06)*invDet,
1148 (-a30*b03 + a31*b01 - a32*b00)*invDet,
1149 (a20*b03 - a21*b01 + a22*b00)*invDet };
1150
1151 // Create quaternion from source point
1152 Quaternion quat = { source.x, source.y, source.z, 1.0f };
1153
1154 // Multiply quat point by unprojected matrix
1155 Quaternion qtransformed = { // QuaternionTransform(quat, matViewProjInv)
1156 matViewProjInv.m0*quat.x + matViewProjInv.m4*quat.y + matViewProjInv.m8*quat.z + matViewProjInv.m12*quat.w,
1157 matViewProjInv.m1*quat.x + matViewProjInv.m5*quat.y + matViewProjInv.m9*quat.z + matViewProjInv.m13*quat.w,
1158 matViewProjInv.m2*quat.x + matViewProjInv.m6*quat.y + matViewProjInv.m10*quat.z + matViewProjInv.m14*quat.w,
1159 matViewProjInv.m3*quat.x + matViewProjInv.m7*quat.y + matViewProjInv.m11*quat.z + matViewProjInv.m15*quat.w };
1160
1161 // Normalized world points in vectors
1162 result.x = qtransformed.x/qtransformed.w;
1163 result.y = qtransformed.y/qtransformed.w;
1164 result.z = qtransformed.z/qtransformed.w;
1165
1166 return result;
1167 }
1168
1169 // Get Vector3 as float array
1170 RMAPI float3 Vector3ToFloatV(Vector3 v)
1171 {
1172 float3 buffer = { 0 };
1173
1174 buffer.v[0] = v.x;
1175 buffer.v[1] = v.y;
1176 buffer.v[2] = v.z;
1177
1178 return buffer;
1179 }
1180
1181 // Invert the given vector
1182 RMAPI Vector3 Vector3Invert(Vector3 v)
1183 {
1184 Vector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };
1185
1186 return result;
1187 }
1188
1189 // Clamp the components of the vector between
1190 // min and max values specified by the given vectors
1191 RMAPI Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max)
1192 {
1193 Vector3 result = { 0 };
1194
1195 result.x = fminf(max.x, fmaxf(min.x, v.x));
1196 result.y = fminf(max.y, fmaxf(min.y, v.y));
1197 result.z = fminf(max.z, fmaxf(min.z, v.z));
1198
1199 return result;
1200 }
1201
1202 // Clamp the magnitude of the vector between two values
1203 RMAPI Vector3 Vector3ClampValue(Vector3 v, float min, float max)
1204 {
1205 Vector3 result = v;
1206
1207 float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
1208 if (length > 0.0f)
1209 {
1210 length = sqrtf(length);
1211
1212 float scale = 1; // By default, 1 as the neutral element
1213 if (length < min) scale = min/length;
1214 else if (length > max) scale = max/length;
1215
1216 result.x = v.x*scale;
1217 result.y = v.y*scale;
1218 result.z = v.z*scale;
1219 }
1220
1221 return result;
1222 }
1223
1224 // Check whether two given vectors are almost equal
1225 RMAPI int Vector3Equals(Vector3 p, Vector3 q)
1226 {
1227 #if !defined(EPSILON)
1228 #define EPSILON 0.000001f
1229 #endif
1230
1231 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
1232 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
1233 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z)))));
1234
1235 return result;
1236 }
1237
1238 // Compute the direction of a refracted ray
1239 // v: normalized direction of the incoming ray
1240 // n: normalized normal vector of the interface of two optical media
1241 // r: ratio of the refractive index of the medium from where the ray comes
1242 // to the refractive index of the medium on the other side of the surface
1243 RMAPI Vector3 Vector3Refract(Vector3 v, Vector3 n, float r)
1244 {
1245 Vector3 result = { 0 };
1246
1247 float dot = v.x*n.x + v.y*n.y + v.z*n.z;
1248 float d = 1.0f - r*r*(1.0f - dot*dot);
1249
1250 if (d >= 0.0f)
1251 {
1252 d = sqrtf(d);
1253 v.x = r*v.x - (r*dot + d)*n.x;
1254 v.y = r*v.y - (r*dot + d)*n.y;
1255 v.z = r*v.z - (r*dot + d)*n.z;
1256
1257 result = v;
1258 }
1259
1260 return result;
1261 }
1262
1263
1264 //----------------------------------------------------------------------------------
1265 // Module Functions Definition - Vector4 math
1266 //----------------------------------------------------------------------------------
1267 // Get vector zero
1268 RMAPI Vector4 Vector4Zero(void)
1269 {
1270 Vector4 result = { 0.0f, 0.0f, 0.0f, 0.0f };
1271 return result;
1272 }
1273
1274 // Get vector one
1275 RMAPI Vector4 Vector4One(void)
1276 {
1277 Vector4 result = { 1.0f, 1.0f, 1.0f, 1.0f };
1278 return result;
1279 }
1280
1281 // Add two vectors
1282 RMAPI Vector4 Vector4Add(Vector4 v1, Vector4 v2)
1283 {
1284 Vector4 result = {
1285 v1.x + v2.x,
1286 v1.y + v2.y,
1287 v1.z + v2.z,
1288 v1.w + v2.w
1289 };
1290 return result;
1291 }
1292
1293 // Add value to vector components
1294 RMAPI Vector4 Vector4AddValue(Vector4 v, float add)
1295 {
1296 Vector4 result = {
1297 v.x + add,
1298 v.y + add,
1299 v.z + add,
1300 v.w + add
1301 };
1302 return result;
1303 }
1304
1305 // Substract vectors
1306 RMAPI Vector4 Vector4Subtract(Vector4 v1, Vector4 v2)
1307 {
1308 Vector4 result = {
1309 v1.x - v2.x,
1310 v1.y - v2.y,
1311 v1.z - v2.z,
1312 v1.w - v2.w
1313 };
1314 return result;
1315 }
1316
1317 // Substract value from vector components
1318 RMAPI Vector4 Vector4SubtractValue(Vector4 v, float add)
1319 {
1320 Vector4 result = {
1321 v.x - add,
1322 v.y - add,
1323 v.z - add,
1324 v.w - add
1325 };
1326 return result;
1327 }
1328
1329 // Vector length
1330 RMAPI float Vector4Length(Vector4 v)
1331 {
1332 float result = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
1333 return result;
1334 }
1335
1336 // Vector square length
1337 RMAPI float Vector4LengthSqr(Vector4 v)
1338 {
1339 float result = (v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w);
1340 return result;
1341 }
1342
1343 // Vectors dot product
1344 RMAPI float Vector4DotProduct(Vector4 v1, Vector4 v2)
1345 {
1346 float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w);
1347 return result;
1348 }
1349
1350 // Calculate distance between two vectors
1351 RMAPI float Vector4Distance(Vector4 v1, Vector4 v2)
1352 {
1353 float result = sqrtf(
1354 (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
1355 (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w));
1356 return result;
1357 }
1358
1359 // Calculate square distance between two vectors
1360 RMAPI float Vector4DistanceSqr(Vector4 v1, Vector4 v2)
1361 {
1362 float result =
1363 (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
1364 (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w);
1365
1366 return result;
1367 }
1368
1369 // Scale vector components by value (multiply)
1370 RMAPI Vector4 Vector4Scale(Vector4 v, float scale)
1371 {
1372 Vector4 result = { v.x*scale, v.y*scale, v.z*scale, v.w*scale };
1373 return result;
1374 }
1375
1376 // Multiply vector by vector
1377 RMAPI Vector4 Vector4Multiply(Vector4 v1, Vector4 v2)
1378 {
1379 Vector4 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z, v1.w*v2.w };
1380 return result;
1381 }
1382
1383 // Negate vector
1384 RMAPI Vector4 Vector4Negate(Vector4 v)
1385 {
1386 Vector4 result = { -v.x, -v.y, -v.z, -v.w };
1387 return result;
1388 }
1389
1390 // Divide vector by vector
1391 RMAPI Vector4 Vector4Divide(Vector4 v1, Vector4 v2)
1392 {
1393 Vector4 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z, v1.w/v2.w };
1394 return result;
1395 }
1396
1397 // Normalize provided vector
1398 RMAPI Vector4 Vector4Normalize(Vector4 v)
1399 {
1400 Vector4 result = { 0 };
1401 float length = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
1402
1403 if (length > 0)
1404 {
1405 float ilength = 1.0f/length;
1406 result.x = v.x*ilength;
1407 result.y = v.y*ilength;
1408 result.z = v.z*ilength;
1409 result.w = v.w*ilength;
1410 }
1411
1412 return result;
1413 }
1414
1415 // Get min value for each pair of components
1416 RMAPI Vector4 Vector4Min(Vector4 v1, Vector4 v2)
1417 {
1418 Vector4 result = { 0 };
1419
1420 result.x = fminf(v1.x, v2.x);
1421 result.y = fminf(v1.y, v2.y);
1422 result.z = fminf(v1.z, v2.z);
1423 result.w = fminf(v1.w, v2.w);
1424
1425 return result;
1426 }
1427
1428 // Get max value for each pair of components
1429 RMAPI Vector4 Vector4Max(Vector4 v1, Vector4 v2)
1430 {
1431 Vector4 result = { 0 };
1432
1433 result.x = fmaxf(v1.x, v2.x);
1434 result.y = fmaxf(v1.y, v2.y);
1435 result.z = fmaxf(v1.z, v2.z);
1436 result.w = fmaxf(v1.w, v2.w);
1437
1438 return result;
1439 }
1440
1441 // Calculate linear interpolation between two vectors
1442 RMAPI Vector4 Vector4Lerp(Vector4 v1, Vector4 v2, float amount)
1443 {
1444 Vector4 result = { 0 };
1445
1446 result.x = v1.x + amount*(v2.x - v1.x);
1447 result.y = v1.y + amount*(v2.y - v1.y);
1448 result.z = v1.z + amount*(v2.z - v1.z);
1449 result.w = v1.w + amount*(v2.w - v1.w);
1450
1451 return result;
1452 }
1453
1454 // Move Vector towards target
1455 RMAPI Vector4 Vector4MoveTowards(Vector4 v, Vector4 target, float maxDistance)
1456 {
1457 Vector4 result = { 0 };
1458
1459 float dx = target.x - v.x;
1460 float dy = target.y - v.y;
1461 float dz = target.z - v.z;
1462 float dw = target.w - v.w;
1463 float value = (dx*dx) + (dy*dy) + (dz*dz) + (dw*dw);
1464
1465 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;
1466
1467 float dist = sqrtf(value);
1468
1469 result.x = v.x + dx/dist*maxDistance;
1470 result.y = v.y + dy/dist*maxDistance;
1471 result.z = v.z + dz/dist*maxDistance;
1472 result.w = v.w + dw/dist*maxDistance;
1473
1474 return result;
1475 }
1476
1477 // Invert the given vector
1478 RMAPI Vector4 Vector4Invert(Vector4 v)
1479 {
1480 Vector4 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z, 1.0f/v.w };
1481 return result;
1482 }
1483
1484 // Check whether two given vectors are almost equal
1485 RMAPI int Vector4Equals(Vector4 p, Vector4 q)
1486 {
1487 #if !defined(EPSILON)
1488 #define EPSILON 0.000001f
1489 #endif
1490
1491 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
1492 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
1493 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
1494 ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))));
1495 return result;
1496 }
1497
1498
1499 //----------------------------------------------------------------------------------
1500 // Module Functions Definition - Matrix math
1501 //----------------------------------------------------------------------------------
1502
1503 // Compute matrix determinant
1504 RMAPI float MatrixDeterminant(Matrix mat)
1505 {
1506 float result = 0.0f;
1507 /*
1508 // Cache the matrix values (speed optimization)
1509 float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
1510 float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
1511 float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
1512 float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
1513
1514 // NOTE: It takes 72 multiplication to calculate 4x4 matrix determinant
1515 result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
1516 a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
1517 a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
1518 a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
1519 a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
1520 a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
1521 */
1522 // Using Laplace expansion (https://en.wikipedia.org/wiki/Laplace_expansion),
1523 // previous operation can be simplified to 40 multiplications, decreasing matrix
1524 // size from 4x4 to 2x2 using minors
1525
1526 // Cache the matrix values (speed optimization)
1527 float m0 = mat.m0, m1 = mat.m1, m2 = mat.m2, m3 = mat.m3;
1528 float m4 = mat.m4, m5 = mat.m5, m6 = mat.m6, m7 = mat.m7;
1529 float m8 = mat.m8, m9 = mat.m9, m10 = mat.m10, m11 = mat.m11;
1530 float m12 = mat.m12, m13 = mat.m13, m14 = mat.m14, m15 = mat.m15;
1531
1532 result = (m0*((m5*(m10*m15 - m11*m14) - m9*(m6*m15 - m7*m14) + m13*(m6*m11 - m7*m10))) -
1533 m4*((m1*(m10*m15 - m11*m14) - m9*(m2*m15 - m3*m14) + m13*(m2*m11 - m3*m10))) +
1534 m8*((m1*(m6*m15 - m7*m14) - m5*(m2*m15 - m3*m14) + m13*(m2*m7 - m3*m6))) -
1535 m12*((m1*(m6*m11 - m7*m10) - m5*(m2*m11 - m3*m10) + m9*(m2*m7 - m3*m6))));
1536
1537 return result;
1538 }
1539
1540 // Get the trace of the matrix (sum of the values along the diagonal)
1541 RMAPI float MatrixTrace(Matrix mat)
1542 {
1543 float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);
1544
1545 return result;
1546 }
1547
1548 // Transposes provided matrix
1549 RMAPI Matrix MatrixTranspose(Matrix mat)
1550 {
1551 Matrix result = { 0 };
1552
1553 result.m0 = mat.m0;
1554 result.m1 = mat.m4;
1555 result.m2 = mat.m8;
1556 result.m3 = mat.m12;
1557 result.m4 = mat.m1;
1558 result.m5 = mat.m5;
1559 result.m6 = mat.m9;
1560 result.m7 = mat.m13;
1561 result.m8 = mat.m2;
1562 result.m9 = mat.m6;
1563 result.m10 = mat.m10;
1564 result.m11 = mat.m14;
1565 result.m12 = mat.m3;
1566 result.m13 = mat.m7;
1567 result.m14 = mat.m11;
1568 result.m15 = mat.m15;
1569
1570 return result;
1571 }
1572
1573 // Invert provided matrix
1574 RMAPI Matrix MatrixInvert(Matrix mat)
1575 {
1576 Matrix result = { 0 };
1577
1578 // Cache the matrix values (speed optimization)
1579 float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
1580 float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
1581 float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
1582 float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
1583
1584 float b00 = a00*a11 - a01*a10;
1585 float b01 = a00*a12 - a02*a10;
1586 float b02 = a00*a13 - a03*a10;
1587 float b03 = a01*a12 - a02*a11;
1588 float b04 = a01*a13 - a03*a11;
1589 float b05 = a02*a13 - a03*a12;
1590 float b06 = a20*a31 - a21*a30;
1591 float b07 = a20*a32 - a22*a30;
1592 float b08 = a20*a33 - a23*a30;
1593 float b09 = a21*a32 - a22*a31;
1594 float b10 = a21*a33 - a23*a31;
1595 float b11 = a22*a33 - a23*a32;
1596
1597 // Calculate the invert determinant (inlined to avoid double-caching)
1598 float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
1599
1600 result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
1601 result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
1602 result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
1603 result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
1604 result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
1605 result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
1606 result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
1607 result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
1608 result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
1609 result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
1610 result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
1611 result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
1612 result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
1613 result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
1614 result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
1615 result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;
1616
1617 return result;
1618 }
1619
1620 // Get identity matrix
1621 RMAPI Matrix MatrixIdentity(void)
1622 {
1623 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1624 0.0f, 1.0f, 0.0f, 0.0f,
1625 0.0f, 0.0f, 1.0f, 0.0f,
1626 0.0f, 0.0f, 0.0f, 1.0f };
1627
1628 return result;
1629 }
1630
1631 // Add two matrices
1632 RMAPI Matrix MatrixAdd(Matrix left, Matrix right)
1633 {
1634 Matrix result = { 0 };
1635
1636 result.m0 = left.m0 + right.m0;
1637 result.m1 = left.m1 + right.m1;
1638 result.m2 = left.m2 + right.m2;
1639 result.m3 = left.m3 + right.m3;
1640 result.m4 = left.m4 + right.m4;
1641 result.m5 = left.m5 + right.m5;
1642 result.m6 = left.m6 + right.m6;
1643 result.m7 = left.m7 + right.m7;
1644 result.m8 = left.m8 + right.m8;
1645 result.m9 = left.m9 + right.m9;
1646 result.m10 = left.m10 + right.m10;
1647 result.m11 = left.m11 + right.m11;
1648 result.m12 = left.m12 + right.m12;
1649 result.m13 = left.m13 + right.m13;
1650 result.m14 = left.m14 + right.m14;
1651 result.m15 = left.m15 + right.m15;
1652
1653 return result;
1654 }
1655
1656 // Subtract two matrices (left - right)
1657 RMAPI Matrix MatrixSubtract(Matrix left, Matrix right)
1658 {
1659 Matrix result = { 0 };
1660
1661 result.m0 = left.m0 - right.m0;
1662 result.m1 = left.m1 - right.m1;
1663 result.m2 = left.m2 - right.m2;
1664 result.m3 = left.m3 - right.m3;
1665 result.m4 = left.m4 - right.m4;
1666 result.m5 = left.m5 - right.m5;
1667 result.m6 = left.m6 - right.m6;
1668 result.m7 = left.m7 - right.m7;
1669 result.m8 = left.m8 - right.m8;
1670 result.m9 = left.m9 - right.m9;
1671 result.m10 = left.m10 - right.m10;
1672 result.m11 = left.m11 - right.m11;
1673 result.m12 = left.m12 - right.m12;
1674 result.m13 = left.m13 - right.m13;
1675 result.m14 = left.m14 - right.m14;
1676 result.m15 = left.m15 - right.m15;
1677
1678 return result;
1679 }
1680
1681 // Get two matrix multiplication
1682 // NOTE: When multiplying matrices... the order matters!
1683 RMAPI Matrix MatrixMultiply(Matrix left, Matrix right)
1684 {
1685 Matrix result = { 0 };
1686
1687 #if defined(RAYMATH_SSE_ENABLED)
1688 // Load left side and right side
1689 __m128 c0 = _mm_set_ps(right.m12, right.m8, right.m4, right.m0);
1690 __m128 c1 = _mm_set_ps(right.m13, right.m9, right.m5, right.m1);
1691 __m128 c2 = _mm_set_ps(right.m14, right.m10, right.m6, right.m2);
1692 __m128 c3 = _mm_set_ps(right.m15, right.m11, right.m7, right.m3);
1693
1694 // Transpose so c0..c3 become *rows* of the right matrix in semantic order
1695 _MM_TRANSPOSE4_PS(c0, c1, c2, c3);
1696
1697 float tmp[4] = { 0 };
1698 __m128 row;
1699
1700 // Row 0 of result: [m0, m1, m2, m3]
1701 row = _mm_mul_ps(_mm_set1_ps(left.m0), c0);
1702 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m1), c1));
1703 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m2), c2));
1704 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m3), c3));
1705 _mm_storeu_ps(tmp, row);
1706 result.m0 = tmp[0];
1707 result.m1 = tmp[1];
1708 result.m2 = tmp[2];
1709 result.m3 = tmp[3];
1710
1711 // Row 1 of result: [m4, m5, m6, m7]
1712 row = _mm_mul_ps(_mm_set1_ps(left.m4), c0);
1713 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m5), c1));
1714 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m6), c2));
1715 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m7), c3));
1716 _mm_storeu_ps(tmp, row);
1717 result.m4 = tmp[0];
1718 result.m5 = tmp[1];
1719 result.m6 = tmp[2];
1720 result.m7 = tmp[3];
1721
1722 // Row 2 of result: [m8, m9, m10, m11]
1723 row = _mm_mul_ps(_mm_set1_ps(left.m8), c0);
1724 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m9), c1));
1725 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m10), c2));
1726 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m11), c3));
1727 _mm_storeu_ps(tmp, row);
1728 result.m8 = tmp[0];
1729 result.m9 = tmp[1];
1730 result.m10 = tmp[2];
1731 result.m11 = tmp[3];
1732
1733 // Row 3 of result: [m12, m13, m14, m15]
1734 row = _mm_mul_ps(_mm_set1_ps(left.m12), c0);
1735 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m13), c1));
1736 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m14), c2));
1737 row = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m15), c3));
1738 _mm_storeu_ps(tmp, row);
1739 result.m12 = tmp[0];
1740 result.m13 = tmp[1];
1741 result.m14 = tmp[2];
1742 result.m15 = tmp[3];
1743 #else
1744 result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
1745 result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
1746 result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
1747 result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
1748 result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
1749 result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
1750 result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
1751 result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
1752 result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
1753 result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
1754 result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
1755 result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
1756 result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
1757 result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
1758 result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
1759 result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
1760 #endif
1761
1762 return result;
1763 }
1764
1765 // Multiply matrix components by value
1766 RMAPI Matrix MatrixMultiplyValue(Matrix left, float value)
1767 {
1768 Matrix result = {
1769 left.m0*value, left.m4*value, left.m8*value, left.m12*value,
1770 left.m1*value, left.m5*value, left.m9*value, left.m13*value,
1771 left.m2*value, left.m6*value, left.m10*value, left.m14*value,
1772 left.m3*value, left.m7*value, left.m11*value, left.m15*value
1773 };
1774
1775 return result;
1776 }
1777
1778 // Get translation matrix
1779 RMAPI Matrix MatrixTranslate(float x, float y, float z)
1780 {
1781 Matrix result = { 1.0f, 0.0f, 0.0f, x,
1782 0.0f, 1.0f, 0.0f, y,
1783 0.0f, 0.0f, 1.0f, z,
1784 0.0f, 0.0f, 0.0f, 1.0f };
1785
1786 return result;
1787 }
1788
1789 // Create rotation matrix from axis and angle
1790 // NOTE: Angle should be provided in radians
1791 RMAPI Matrix MatrixRotate(Vector3 axis, float angle)
1792 {
1793 Matrix result = { 0 };
1794
1795 float x = axis.x, y = axis.y, z = axis.z;
1796
1797 float lengthSquared = x*x + y*y + z*z;
1798
1799 if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
1800 {
1801 float ilength = 1.0f/sqrtf(lengthSquared);
1802 x *= ilength;
1803 y *= ilength;
1804 z *= ilength;
1805 }
1806
1807 float sinres = sinf(angle);
1808 float cosres = cosf(angle);
1809 float t = 1.0f - cosres;
1810
1811 result.m0 = x*x*t + cosres;
1812 result.m1 = y*x*t + z*sinres;
1813 result.m2 = z*x*t - y*sinres;
1814 result.m3 = 0.0f;
1815
1816 result.m4 = x*y*t - z*sinres;
1817 result.m5 = y*y*t + cosres;
1818 result.m6 = z*y*t + x*sinres;
1819 result.m7 = 0.0f;
1820
1821 result.m8 = x*z*t + y*sinres;
1822 result.m9 = y*z*t - x*sinres;
1823 result.m10 = z*z*t + cosres;
1824 result.m11 = 0.0f;
1825
1826 result.m12 = 0.0f;
1827 result.m13 = 0.0f;
1828 result.m14 = 0.0f;
1829 result.m15 = 1.0f;
1830
1831 return result;
1832 }
1833
1834 // Get x-rotation matrix
1835 // NOTE: Angle must be provided in radians
1836 RMAPI Matrix MatrixRotateX(float angle)
1837 {
1838 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1839 0.0f, 1.0f, 0.0f, 0.0f,
1840 0.0f, 0.0f, 1.0f, 0.0f,
1841 0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()
1842
1843 float cosres = cosf(angle);
1844 float sinres = sinf(angle);
1845
1846 result.m5 = cosres;
1847 result.m6 = sinres;
1848 result.m9 = -sinres;
1849 result.m10 = cosres;
1850
1851 return result;
1852 }
1853
1854 // Get y-rotation matrix
1855 // NOTE: Angle must be provided in radians
1856 RMAPI Matrix MatrixRotateY(float angle)
1857 {
1858 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1859 0.0f, 1.0f, 0.0f, 0.0f,
1860 0.0f, 0.0f, 1.0f, 0.0f,
1861 0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()
1862
1863 float cosres = cosf(angle);
1864 float sinres = sinf(angle);
1865
1866 result.m0 = cosres;
1867 result.m2 = -sinres;
1868 result.m8 = sinres;
1869 result.m10 = cosres;
1870
1871 return result;
1872 }
1873
1874 // Get z-rotation matrix
1875 // NOTE: Angle must be provided in radians
1876 RMAPI Matrix MatrixRotateZ(float angle)
1877 {
1878 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1879 0.0f, 1.0f, 0.0f, 0.0f,
1880 0.0f, 0.0f, 1.0f, 0.0f,
1881 0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()
1882
1883 float cosres = cosf(angle);
1884 float sinres = sinf(angle);
1885
1886 result.m0 = cosres;
1887 result.m1 = sinres;
1888 result.m4 = -sinres;
1889 result.m5 = cosres;
1890
1891 return result;
1892 }
1893
1894
1895 // Get xyz-rotation matrix
1896 // NOTE: Angle must be provided in radians
1897 RMAPI Matrix MatrixRotateXYZ(Vector3 angle)
1898 {
1899 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1900 0.0f, 1.0f, 0.0f, 0.0f,
1901 0.0f, 0.0f, 1.0f, 0.0f,
1902 0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()
1903
1904 float cosz = cosf(-angle.z);
1905 float sinz = sinf(-angle.z);
1906 float cosy = cosf(-angle.y);
1907 float siny = sinf(-angle.y);
1908 float cosx = cosf(-angle.x);
1909 float sinx = sinf(-angle.x);
1910
1911 result.m0 = cosz*cosy;
1912 result.m1 = (cosz*siny*sinx) - (sinz*cosx);
1913 result.m2 = (cosz*siny*cosx) + (sinz*sinx);
1914
1915 result.m4 = sinz*cosy;
1916 result.m5 = (sinz*siny*sinx) + (cosz*cosx);
1917 result.m6 = (sinz*siny*cosx) - (cosz*sinx);
1918
1919 result.m8 = -siny;
1920 result.m9 = cosy*sinx;
1921 result.m10= cosy*cosx;
1922
1923 return result;
1924 }
1925
1926 // Get zyx-rotation matrix
1927 // NOTE: Angle must be provided in radians
1928 RMAPI Matrix MatrixRotateZYX(Vector3 angle)
1929 {
1930 Matrix result = { 0 };
1931
1932 float cz = cosf(angle.z);
1933 float sz = sinf(angle.z);
1934 float cy = cosf(angle.y);
1935 float sy = sinf(angle.y);
1936 float cx = cosf(angle.x);
1937 float sx = sinf(angle.x);
1938
1939 result.m0 = cz*cy;
1940 result.m4 = cz*sy*sx - cx*sz;
1941 result.m8 = sz*sx + cz*cx*sy;
1942 result.m12 = 0;
1943
1944 result.m1 = cy*sz;
1945 result.m5 = cz*cx + sz*sy*sx;
1946 result.m9 = cx*sz*sy - cz*sx;
1947 result.m13 = 0;
1948
1949 result.m2 = -sy;
1950 result.m6 = cy*sx;
1951 result.m10 = cy*cx;
1952 result.m14 = 0;
1953
1954 result.m3 = 0;
1955 result.m7 = 0;
1956 result.m11 = 0;
1957 result.m15 = 1;
1958
1959 return result;
1960 }
1961
1962 // Get scaling matrix
1963 RMAPI Matrix MatrixScale(float x, float y, float z)
1964 {
1965 Matrix result = { x, 0.0f, 0.0f, 0.0f,
1966 0.0f, y, 0.0f, 0.0f,
1967 0.0f, 0.0f, z, 0.0f,
1968 0.0f, 0.0f, 0.0f, 1.0f };
1969
1970 return result;
1971 }
1972
1973 // Get perspective projection matrix
1974 RMAPI Matrix MatrixFrustum(float left, float right, float bottom, float top, float nearPlane, float farPlane)
1975 {
1976 Matrix result = { 0 };
1977
1978 float rl = right - left;
1979 float tb = top - bottom;
1980 float fn = farPlane - nearPlane;
1981
1982 result.m0 = (nearPlane*2.0f)/rl;
1983 result.m1 = 0.0f;
1984 result.m2 = 0.0f;
1985 result.m3 = 0.0f;
1986
1987 result.m4 = 0.0f;
1988 result.m5 = (nearPlane*2.0f)/tb;
1989 result.m6 = 0.0f;
1990 result.m7 = 0.0f;
1991
1992 result.m8 = (right + left)/rl;
1993 result.m9 = (top + bottom)/tb;
1994 result.m10 = -(farPlane + nearPlane)/fn;
1995 result.m11 = -1.0f;
1996
1997 result.m12 = 0.0f;
1998 result.m13 = 0.0f;
1999 result.m14 = -(farPlane*nearPlane*2.0f)/fn;
2000 result.m15 = 0.0f;
2001
2002 return result;
2003 }
2004
2005 // Get perspective projection matrix
2006 // NOTE: Fovy angle must be provided in radians
2007 RMAPI Matrix MatrixPerspective(float fovY, float aspect, float nearPlane, float farPlane)
2008 {
2009 Matrix result = { 0 };
2010
2011 float top = nearPlane*tanf(fovY*0.5f);
2012 float bottom = -top;
2013 float right = top*aspect;
2014 float left = -right;
2015
2016 // MatrixFrustum(-right, right, -top, top, near, far);
2017 float rl = right - left;
2018 float tb = top - bottom;
2019 float fn = farPlane - nearPlane;
2020
2021 result.m0 = (nearPlane*2.0f)/rl;
2022 result.m5 = (nearPlane*2.0f)/tb;
2023 result.m8 = (right + left)/rl;
2024 result.m9 = (top + bottom)/tb;
2025 result.m10 = -(farPlane + nearPlane)/fn;
2026 result.m11 = -1.0f;
2027 result.m14 = -(farPlane*nearPlane*2.0f)/fn;
2028
2029 return result;
2030 }
2031
2032 // Get orthographic projection matrix
2033 RMAPI Matrix MatrixOrtho(float left, float right, float bottom, float top, float nearPlane, float farPlane)
2034 {
2035 Matrix result = { 0 };
2036
2037 float rl = right - left;
2038 float tb = top - bottom;
2039 float fn = farPlane - nearPlane;
2040
2041 result.m0 = 2.0f/rl;
2042 result.m1 = 0.0f;
2043 result.m2 = 0.0f;
2044 result.m3 = 0.0f;
2045 result.m4 = 0.0f;
2046 result.m5 = 2.0f/tb;
2047 result.m6 = 0.0f;
2048 result.m7 = 0.0f;
2049 result.m8 = 0.0f;
2050 result.m9 = 0.0f;
2051 result.m10 = -2.0f/fn;
2052 result.m11 = 0.0f;
2053 result.m12 = -(left + right)/rl;
2054 result.m13 = -(top + bottom)/tb;
2055 result.m14 = -(farPlane + nearPlane)/fn;
2056 result.m15 = 1.0f;
2057
2058 return result;
2059 }
2060
2061 // Get camera look-at matrix (view matrix)
2062 RMAPI Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
2063 {
2064 Matrix result = { 0 };
2065
2066 float length = 0.0f;
2067 float ilength = 0.0f;
2068
2069 // Vector3Subtract(eye, target)
2070 Vector3 vz = { eye.x - target.x, eye.y - target.y, eye.z - target.z };
2071
2072 // Vector3Normalize(vz)
2073 Vector3 v = vz;
2074 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
2075 if (length == 0.0f) length = 1.0f;
2076 ilength = 1.0f/length;
2077 vz.x *= ilength;
2078 vz.y *= ilength;
2079 vz.z *= ilength;
2080
2081 // Vector3CrossProduct(up, vz)
2082 Vector3 vx = { up.y*vz.z - up.z*vz.y, up.z*vz.x - up.x*vz.z, up.x*vz.y - up.y*vz.x };
2083
2084 // Vector3Normalize(x)
2085 v = vx;
2086 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
2087 if (length == 0.0f) length = 1.0f;
2088 ilength = 1.0f/length;
2089 vx.x *= ilength;
2090 vx.y *= ilength;
2091 vx.z *= ilength;
2092
2093 // Vector3CrossProduct(vz, vx)
2094 Vector3 vy = { vz.y*vx.z - vz.z*vx.y, vz.z*vx.x - vz.x*vx.z, vz.x*vx.y - vz.y*vx.x };
2095
2096 result.m0 = vx.x;
2097 result.m1 = vy.x;
2098 result.m2 = vz.x;
2099 result.m3 = 0.0f;
2100 result.m4 = vx.y;
2101 result.m5 = vy.y;
2102 result.m6 = vz.y;
2103 result.m7 = 0.0f;
2104 result.m8 = vx.z;
2105 result.m9 = vy.z;
2106 result.m10 = vz.z;
2107 result.m11 = 0.0f;
2108 result.m12 = -(vx.x*eye.x + vx.y*eye.y + vx.z*eye.z); // Vector3DotProduct(vx, eye)
2109 result.m13 = -(vy.x*eye.x + vy.y*eye.y + vy.z*eye.z); // Vector3DotProduct(vy, eye)
2110 result.m14 = -(vz.x*eye.x + vz.y*eye.y + vz.z*eye.z); // Vector3DotProduct(vz, eye)
2111 result.m15 = 1.0f;
2112
2113 return result;
2114 }
2115
2116 // Get float array of matrix data
2117 RMAPI float16 MatrixToFloatV(Matrix mat)
2118 {
2119 float16 result = { 0 };
2120
2121 result.v[0] = mat.m0;
2122 result.v[1] = mat.m1;
2123 result.v[2] = mat.m2;
2124 result.v[3] = mat.m3;
2125 result.v[4] = mat.m4;
2126 result.v[5] = mat.m5;
2127 result.v[6] = mat.m6;
2128 result.v[7] = mat.m7;
2129 result.v[8] = mat.m8;
2130 result.v[9] = mat.m9;
2131 result.v[10] = mat.m10;
2132 result.v[11] = mat.m11;
2133 result.v[12] = mat.m12;
2134 result.v[13] = mat.m13;
2135 result.v[14] = mat.m14;
2136 result.v[15] = mat.m15;
2137
2138 return result;
2139 }
2140
2141 //----------------------------------------------------------------------------------
2142 // Module Functions Definition - Quaternion math
2143 //----------------------------------------------------------------------------------
2144
2145 // Add two quaternions
2146 RMAPI Quaternion QuaternionAdd(Quaternion q1, Quaternion q2)
2147 {
2148 Quaternion result = {q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w};
2149
2150 return result;
2151 }
2152
2153 // Add quaternion and float value
2154 RMAPI Quaternion QuaternionAddValue(Quaternion q, float add)
2155 {
2156 Quaternion result = {q.x + add, q.y + add, q.z + add, q.w + add};
2157
2158 return result;
2159 }
2160
2161 // Subtract two quaternions
2162 RMAPI Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2)
2163 {
2164 Quaternion result = {q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w};
2165
2166 return result;
2167 }
2168
2169 // Subtract quaternion and float value
2170 RMAPI Quaternion QuaternionSubtractValue(Quaternion q, float sub)
2171 {
2172 Quaternion result = {q.x - sub, q.y - sub, q.z - sub, q.w - sub};
2173
2174 return result;
2175 }
2176
2177 // Get identity quaternion
2178 RMAPI Quaternion QuaternionIdentity(void)
2179 {
2180 Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
2181
2182 return result;
2183 }
2184
2185 // Computes the length of a quaternion
2186 RMAPI float QuaternionLength(Quaternion q)
2187 {
2188 float result = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2189
2190 return result;
2191 }
2192
2193 // Normalize provided quaternion
2194 RMAPI Quaternion QuaternionNormalize(Quaternion q)
2195 {
2196 Quaternion result = { 0 };
2197
2198 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2199 if (length == 0.0f) length = 1.0f;
2200 float ilength = 1.0f/length;
2201
2202 result.x = q.x*ilength;
2203 result.y = q.y*ilength;
2204 result.z = q.z*ilength;
2205 result.w = q.w*ilength;
2206
2207 return result;
2208 }
2209
2210 // Invert provided quaternion
2211 RMAPI Quaternion QuaternionInvert(Quaternion q)
2212 {
2213 Quaternion result = q;
2214
2215 float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
2216
2217 if (lengthSq != 0.0f)
2218 {
2219 float invLength = 1.0f/lengthSq;
2220
2221 result.x *= -invLength;
2222 result.y *= -invLength;
2223 result.z *= -invLength;
2224 result.w *= invLength;
2225 }
2226
2227 return result;
2228 }
2229
2230 // Calculate two quaternion multiplication
2231 RMAPI Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2)
2232 {
2233 Quaternion result = { 0 };
2234
2235 float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w;
2236 float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w;
2237
2238 result.x = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
2239 result.y = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
2240 result.z = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
2241 result.w = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;
2242
2243 return result;
2244 }
2245
2246 // Scale quaternion by float value
2247 RMAPI Quaternion QuaternionScale(Quaternion q, float mul)
2248 {
2249 Quaternion result = { 0 };
2250
2251 result.x = q.x*mul;
2252 result.y = q.y*mul;
2253 result.z = q.z*mul;
2254 result.w = q.w*mul;
2255
2256 return result;
2257 }
2258
2259 // Divide two quaternions
2260 RMAPI Quaternion QuaternionDivide(Quaternion q1, Quaternion q2)
2261 {
2262 Quaternion result = { q1.x/q2.x, q1.y/q2.y, q1.z/q2.z, q1.w/q2.w };
2263
2264 return result;
2265 }
2266
2267 // Calculate linear interpolation between two quaternions
2268 RMAPI Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount)
2269 {
2270 Quaternion result = { 0 };
2271
2272 result.x = q1.x + amount*(q2.x - q1.x);
2273 result.y = q1.y + amount*(q2.y - q1.y);
2274 result.z = q1.z + amount*(q2.z - q1.z);
2275 result.w = q1.w + amount*(q2.w - q1.w);
2276
2277 return result;
2278 }
2279
2280 // Calculate slerp-optimized interpolation between two quaternions
2281 RMAPI Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount)
2282 {
2283 Quaternion result = { 0 };
2284
2285 // q2 and -q2 are the same rotation, take the shorter path like QuaternionSlerp()
2286 if ((q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w) < 0.0f)
2287 {
2288 q2.x = -q2.x; q2.y = -q2.y; q2.z = -q2.z; q2.w = -q2.w;
2289 }
2290
2291 // QuaternionLerp(q1, q2, amount)
2292 result.x = q1.x + amount*(q2.x - q1.x);
2293 result.y = q1.y + amount*(q2.y - q1.y);
2294 result.z = q1.z + amount*(q2.z - q1.z);
2295 result.w = q1.w + amount*(q2.w - q1.w);
2296
2297 // QuaternionNormalize(q);
2298 Quaternion q = result;
2299 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2300 if (length == 0.0f) length = 1.0f;
2301 float ilength = 1.0f/length;
2302
2303 result.x = q.x*ilength;
2304 result.y = q.y*ilength;
2305 result.z = q.z*ilength;
2306 result.w = q.w*ilength;
2307
2308 return result;
2309 }
2310
2311 // Calculates spherical linear interpolation between two quaternions
2312 RMAPI Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount)
2313 {
2314 Quaternion result = { 0 };
2315
2316 #if !defined(EPSILON)
2317 #define EPSILON 0.000001f
2318 #endif
2319
2320 float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
2321
2322 if (cosHalfTheta < 0)
2323 {
2324 q2.x = -q2.x; q2.y = -q2.y; q2.z = -q2.z; q2.w = -q2.w;
2325 cosHalfTheta = -cosHalfTheta;
2326 }
2327
2328 if (fabsf(cosHalfTheta) >= 1.0f) result = q1;
2329 else if (cosHalfTheta > 0.95f) result = QuaternionNlerp(q1, q2, amount);
2330 else
2331 {
2332 float halfTheta = acosf(cosHalfTheta);
2333 float sinHalfTheta = sqrtf(1.0f - cosHalfTheta*cosHalfTheta);
2334
2335 if (fabsf(sinHalfTheta) < EPSILON)
2336 {
2337 result.x = (q1.x*0.5f + q2.x*0.5f);
2338 result.y = (q1.y*0.5f + q2.y*0.5f);
2339 result.z = (q1.z*0.5f + q2.z*0.5f);
2340 result.w = (q1.w*0.5f + q2.w*0.5f);
2341 }
2342 else
2343 {
2344 float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta;
2345 float ratioB = sinf(amount*halfTheta)/sinHalfTheta;
2346
2347 result.x = (q1.x*ratioA + q2.x*ratioB);
2348 result.y = (q1.y*ratioA + q2.y*ratioB);
2349 result.z = (q1.z*ratioA + q2.z*ratioB);
2350 result.w = (q1.w*ratioA + q2.w*ratioB);
2351 }
2352 }
2353
2354 return result;
2355 }
2356
2357 // Calculate quaternion cubic spline interpolation using Cubic Hermite Spline algorithm
2358 // as described in the GLTF 2.0 specification: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#interpolation-cubic
2359 RMAPI Quaternion QuaternionCubicHermiteSpline(Quaternion q1, Quaternion outTangent1, Quaternion q2, Quaternion inTangent2, float t)
2360 {
2361 float t2 = t*t;
2362 float t3 = t2*t;
2363 float h00 = 2*t3 - 3*t2 + 1;
2364 float h10 = t3 - 2*t2 + t;
2365 float h01 = -2*t3 + 3*t2;
2366 float h11 = t3 - t2;
2367
2368 Quaternion p0 = QuaternionScale(q1, h00);
2369 Quaternion m0 = QuaternionScale(outTangent1, h10);
2370 Quaternion p1 = QuaternionScale(q2, h01);
2371 Quaternion m1 = QuaternionScale(inTangent2, h11);
2372
2373 Quaternion result = { 0 };
2374
2375 result = QuaternionAdd(p0, m0);
2376 result = QuaternionAdd(result, p1);
2377 result = QuaternionAdd(result, m1);
2378 result = QuaternionNormalize(result);
2379
2380 return result;
2381 }
2382
2383 // Calculate quaternion based on the rotation from one vector to another
2384 RMAPI Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
2385 {
2386 Quaternion result = { 0 };
2387
2388 float cos2Theta = (from.x*to.x + from.y*to.y + from.z*to.z); // Vector3DotProduct(from, to)
2389 Vector3 cross = { from.y*to.z - from.z*to.y, from.z*to.x - from.x*to.z, from.x*to.y - from.y*to.x }; // Vector3CrossProduct(from, to)
2390
2391 float lengths = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z + cos2Theta*cos2Theta); // |from|*|to|
2392
2393 result.x = cross.x;
2394 result.y = cross.y;
2395 result.z = cross.z;
2396 result.w = lengths + cos2Theta;
2397
2398 // Opposite vectors: cross product is zero, rotate 180 degrees around any axis perpendicular to 'from'
2399 if ((lengths > 0.0f) && (result.w <= lengths*EPSILON))
2400 {
2401 if (fabsf(from.x) > fabsf(from.z))
2402 {
2403 result.x = -from.y;
2404 result.y = from.x;
2405 result.z = 0.0f;
2406 }
2407 else
2408 {
2409 result.x = 0.0f;
2410 result.y = -from.z;
2411 result.z = from.y;
2412 }
2413
2414 result.w = 0.0f;
2415 }
2416
2417 // QuaternionNormalize(q);
2418 // NOTE: Normalize to essentially nlerp the original and identity to 0.5
2419 Quaternion q = result;
2420 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2421 if (length == 0.0f) length = 1.0f;
2422 float ilength = 1.0f/length;
2423
2424 result.x = q.x*ilength;
2425 result.y = q.y*ilength;
2426 result.z = q.z*ilength;
2427 result.w = q.w*ilength;
2428
2429 return result;
2430 }
2431
2432 // Get a quaternion for a given rotation matrix
2433 RMAPI Quaternion QuaternionFromMatrix(Matrix mat)
2434 {
2435 Quaternion result = { 0 };
2436
2437 float fourWSquaredMinus1 = mat.m0 + mat.m5 + mat.m10;
2438 float fourXSquaredMinus1 = mat.m0 - mat.m5 - mat.m10;
2439 float fourYSquaredMinus1 = mat.m5 - mat.m0 - mat.m10;
2440 float fourZSquaredMinus1 = mat.m10 - mat.m0 - mat.m5;
2441
2442 int biggestIndex = 0;
2443 float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
2444 if (fourXSquaredMinus1 > fourBiggestSquaredMinus1)
2445 {
2446 fourBiggestSquaredMinus1 = fourXSquaredMinus1;
2447 biggestIndex = 1;
2448 }
2449
2450 if (fourYSquaredMinus1 > fourBiggestSquaredMinus1)
2451 {
2452 fourBiggestSquaredMinus1 = fourYSquaredMinus1;
2453 biggestIndex = 2;
2454 }
2455
2456 if (fourZSquaredMinus1 > fourBiggestSquaredMinus1)
2457 {
2458 fourBiggestSquaredMinus1 = fourZSquaredMinus1;
2459 biggestIndex = 3;
2460 }
2461
2462 float biggestVal = sqrtf(fourBiggestSquaredMinus1 + 1.0f)*0.5f;
2463 float mult = 0.25f/biggestVal;
2464
2465 switch (biggestIndex)
2466 {
2467 case 0:
2468 result.w = biggestVal;
2469 result.x = (mat.m6 - mat.m9)*mult;
2470 result.y = (mat.m8 - mat.m2)*mult;
2471 result.z = (mat.m1 - mat.m4)*mult;
2472 break;
2473 case 1:
2474 result.x = biggestVal;
2475 result.w = (mat.m6 - mat.m9)*mult;
2476 result.y = (mat.m1 + mat.m4)*mult;
2477 result.z = (mat.m8 + mat.m2)*mult;
2478 break;
2479 case 2:
2480 result.y = biggestVal;
2481 result.w = (mat.m8 - mat.m2)*mult;
2482 result.x = (mat.m1 + mat.m4)*mult;
2483 result.z = (mat.m6 + mat.m9)*mult;
2484 break;
2485 case 3:
2486 result.z = biggestVal;
2487 result.w = (mat.m1 - mat.m4)*mult;
2488 result.x = (mat.m8 + mat.m2)*mult;
2489 result.y = (mat.m6 + mat.m9)*mult;
2490 break;
2491 }
2492
2493 return result;
2494 }
2495
2496 // Get a matrix for a given quaternion
2497 RMAPI Matrix QuaternionToMatrix(Quaternion q)
2498 {
2499 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
2500 0.0f, 1.0f, 0.0f, 0.0f,
2501 0.0f, 0.0f, 1.0f, 0.0f,
2502 0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()
2503
2504 float a2 = q.x*q.x;
2505 float b2 = q.y*q.y;
2506 float c2 = q.z*q.z;
2507 float ac = q.x*q.z;
2508 float ab = q.x*q.y;
2509 float bc = q.y*q.z;
2510 float ad = q.w*q.x;
2511 float bd = q.w*q.y;
2512 float cd = q.w*q.z;
2513
2514 result.m0 = 1 - 2*(b2 + c2);
2515 result.m1 = 2*(ab + cd);
2516 result.m2 = 2*(ac - bd);
2517
2518 result.m4 = 2*(ab - cd);
2519 result.m5 = 1 - 2*(a2 + c2);
2520 result.m6 = 2*(bc + ad);
2521
2522 result.m8 = 2*(ac + bd);
2523 result.m9 = 2*(bc - ad);
2524 result.m10 = 1 - 2*(a2 + b2);
2525
2526 return result;
2527 }
2528
2529 // Get rotation quaternion for an angle and axis
2530 // NOTE: Angle must be provided in radians
2531 RMAPI Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle)
2532 {
2533 Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
2534
2535 float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
2536
2537 if (length != 0.0f)
2538 {
2539 angle *= 0.5f;
2540
2541 // Vector3Normalize(axis)
2542 float ilength = 1.0f/length;
2543 axis.x *= ilength;
2544 axis.y *= ilength;
2545 axis.z *= ilength;
2546
2547 float sinres = sinf(angle);
2548 float cosres = cosf(angle);
2549
2550 result.x = axis.x*sinres;
2551 result.y = axis.y*sinres;
2552 result.z = axis.z*sinres;
2553 result.w = cosres;
2554
2555 // QuaternionNormalize(q);
2556 Quaternion q = result;
2557 length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2558 if (length == 0.0f) length = 1.0f;
2559 ilength = 1.0f/length;
2560 result.x = q.x*ilength;
2561 result.y = q.y*ilength;
2562 result.z = q.z*ilength;
2563 result.w = q.w*ilength;
2564 }
2565
2566 return result;
2567 }
2568
2569 // Get the rotation angle and axis for a given quaternion
2570 RMAPI void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle)
2571 {
2572 if (fabsf(q.w) > 1.0f)
2573 {
2574 // QuaternionNormalize(q);
2575 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2576 if (length == 0.0f) length = 1.0f;
2577 float ilength = 1.0f/length;
2578
2579 q.x = q.x*ilength;
2580 q.y = q.y*ilength;
2581 q.z = q.z*ilength;
2582 q.w = q.w*ilength;
2583 }
2584
2585 Vector3 resAxis = { 0.0f, 0.0f, 0.0f };
2586 float resAngle = 2.0f*acosf(q.w);
2587 float den = sqrtf(1.0f - q.w*q.w);
2588
2589 if (den > EPSILON)
2590 {
2591 resAxis.x = q.x/den;
2592 resAxis.y = q.y/den;
2593 resAxis.z = q.z/den;
2594 }
2595 else
2596 {
2597 // This occurs when the angle is zero
2598 // Not a problem, set an arbitrary normalized axis
2599 resAxis.x = 1.0f;
2600 }
2601
2602 *outAxis = resAxis;
2603 *outAngle = resAngle;
2604 }
2605
2606 // Get the quaternion equivalent to Euler angles
2607 // NOTE: Rotation order is ZYX
2608 RMAPI Quaternion QuaternionFromEuler(float pitch, float yaw, float roll)
2609 {
2610 Quaternion result = { 0 };
2611
2612 float x0 = cosf(pitch*0.5f);
2613 float x1 = sinf(pitch*0.5f);
2614 float y0 = cosf(yaw*0.5f);
2615 float y1 = sinf(yaw*0.5f);
2616 float z0 = cosf(roll*0.5f);
2617 float z1 = sinf(roll*0.5f);
2618
2619 result.x = x1*y0*z0 - x0*y1*z1;
2620 result.y = x0*y1*z0 + x1*y0*z1;
2621 result.z = x0*y0*z1 - x1*y1*z0;
2622 result.w = x0*y0*z0 + x1*y1*z1;
2623
2624 return result;
2625 }
2626
2627 // Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
2628 // NOTE: Angles are returned in a Vector3 struct in radians
2629 RMAPI Vector3 QuaternionToEuler(Quaternion q)
2630 {
2631 Vector3 result = { 0 };
2632
2633 // Pitch (y-axis rotation)
2634 float y0 = 2.0f*(q.w*q.y - q.z*q.x);
2635 y0 = y0 > 1.0f ? 1.0f : y0;
2636 y0 = y0 < -1.0f ? -1.0f : y0;
2637 result.y = asinf(y0);
2638
2639 if (fabsf(y0) >= 0.9999995f)
2640 {
2641 // Gimbal lock: roll and yaw turn about the same axis, keep the whole turn in roll
2642 float sign = (q.w < 0.0f)? -1.0f : 1.0f;
2643 result.x = 2.0f*atan2f(sign*q.x, sign*q.w);
2644 }
2645 else
2646 {
2647 // Roll (x-axis rotation)
2648 float x0 = 2.0f*(q.w*q.x + q.y*q.z);
2649 float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y);
2650 result.x = atan2f(x0, x1);
2651
2652 // Yaw (z-axis rotation)
2653 float z0 = 2.0f*(q.w*q.z + q.x*q.y);
2654 float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z);
2655 result.z = atan2f(z0, z1);
2656 }
2657
2658 return result;
2659 }
2660
2661 // Transform a quaternion given a transformation matrix
2662 RMAPI Quaternion QuaternionTransform(Quaternion q, Matrix mat)
2663 {
2664 Quaternion result = { 0 };
2665
2666 result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w;
2667 result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w;
2668 result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w;
2669 result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w;
2670
2671 return result;
2672 }
2673
2674 // Check whether two given quaternions are almost equal
2675 RMAPI int QuaternionEquals(Quaternion p, Quaternion q)
2676 {
2677 #if !defined(EPSILON)
2678 #define EPSILON 0.000001f
2679 #endif
2680
2681 int result = (((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
2682 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
2683 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
2684 ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))))) ||
2685 (((fabsf(p.x + q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
2686 ((fabsf(p.y + q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
2687 ((fabsf(p.z + q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
2688 ((fabsf(p.w + q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w))))));
2689
2690 return result;
2691 }
2692
2693 // Compose a transformation matrix from rotational, translational and scaling components
2694 RMAPI Matrix MatrixCompose(Vector3 translation, Quaternion rotation, Vector3 scale)
2695 {
2696 // Initialize vectors
2697 Vector3 right = { 1.0f, 0.0f, 0.0f };
2698 Vector3 up = { 0.0f, 1.0f, 0.0f };
2699 Vector3 forward = { 0.0f, 0.0f, 1.0f };
2700
2701 // Scale vectors
2702 right.x *= scale.x;
2703 right.y *= scale.x;
2704 right.z *= scale.x;
2705
2706 up.x *= scale.y;
2707 up.y *= scale.y;
2708 up.z *= scale.y;
2709
2710 forward.x *= scale.z;
2711 forward.y *= scale.z;
2712 forward.z *= scale.z;
2713
2714 // Rotate vectors
2715 // NOTE: A copy of each vector is required, every rotated component depends on all original components
2716 Vector3 temp = right;
2717 //right = Vector3RotateByQuaternion(right, rotation);
2718 right.x = temp.x*(rotation.x*rotation.x + rotation.w*rotation.w - rotation.y*rotation.y - rotation.z*rotation.z) + temp.y*(2*rotation.x*rotation.y - 2*rotation.w*rotation.z) + temp.z*(2*rotation.x*rotation.z + 2*rotation.w*rotation.y);
2719 right.y = temp.x*(2*rotation.w*rotation.z + 2*rotation.x*rotation.y) + temp.y*(rotation.w*rotation.w - rotation.x*rotation.x + rotation.y*rotation.y - rotation.z*rotation.z) + temp.z*(-2*rotation.w*rotation.x + 2*rotation.y*rotation.z);
2720 right.z = temp.x*(-2*rotation.w*rotation.y + 2*rotation.x*rotation.z) + temp.y*(2*rotation.w*rotation.x + 2*rotation.y*rotation.z)+ temp.z*(rotation.w*rotation.w - rotation.x*rotation.x - rotation.y*rotation.y + rotation.z*rotation.z);
2721
2722 temp = up;
2723 //up = Vector3RotateByQuaternion(up, rotation);
2724 up.x = temp.x*(rotation.x*rotation.x + rotation.w*rotation.w - rotation.y*rotation.y - rotation.z*rotation.z) + temp.y*(2*rotation.x*rotation.y - 2*rotation.w*rotation.z) + temp.z*(2*rotation.x*rotation.z + 2*rotation.w*rotation.y);
2725 up.y = temp.x*(2*rotation.w*rotation.z + 2*rotation.x*rotation.y) + temp.y*(rotation.w*rotation.w - rotation.x*rotation.x + rotation.y*rotation.y - rotation.z*rotation.z) + temp.z*(-2*rotation.w*rotation.x + 2*rotation.y*rotation.z);
2726 up.z = temp.x*(-2*rotation.w*rotation.y + 2*rotation.x*rotation.z) + temp.y*(2*rotation.w*rotation.x + 2*rotation.y*rotation.z)+ temp.z*(rotation.w*rotation.w - rotation.x*rotation.x - rotation.y*rotation.y + rotation.z*rotation.z);
2727
2728 temp = forward;
2729 //forward = Vector3RotateByQuaternion(forward, rotation);
2730 forward.x = temp.x*(rotation.x*rotation.x + rotation.w*rotation.w - rotation.y*rotation.y - rotation.z*rotation.z) + temp.y*(2*rotation.x*rotation.y - 2*rotation.w*rotation.z) + temp.z*(2*rotation.x*rotation.z + 2*rotation.w*rotation.y);
2731 forward.y = temp.x*(2*rotation.w*rotation.z + 2*rotation.x*rotation.y) + temp.y*(rotation.w*rotation.w - rotation.x*rotation.x + rotation.y*rotation.y - rotation.z*rotation.z) + temp.z*(-2*rotation.w*rotation.x + 2*rotation.y*rotation.z);
2732 forward.z = temp.x*(-2*rotation.w*rotation.y + 2*rotation.x*rotation.z) + temp.y*(2*rotation.w*rotation.x + 2*rotation.y*rotation.z)+ temp.z*(rotation.w*rotation.w - rotation.x*rotation.x - rotation.y*rotation.y + rotation.z*rotation.z);
2733
2734 // Set result matrix output
2735 Matrix result = {
2736 right.x, up.x, forward.x, translation.x,
2737 right.y, up.y, forward.y, translation.y,
2738 right.z, up.z, forward.z, translation.z,
2739 0.0f, 0.0f, 0.0f, 1.0f
2740 };
2741
2742 return result;
2743 }
2744
2745 // Decompose a transformation matrix into its rotational, translational and scaling components and remove shear
2746 // TODO: WARNING: Following raymath convention and make the function self-contained
2747 RMAPI void MatrixDecompose(Matrix mat, Vector3 *translation, Quaternion *rotation, Vector3 *scale)
2748 {
2749 float eps = (float)1e-9;
2750
2751 // Extract Translation
2752 translation->x = mat.m12;
2753 translation->y = mat.m13;
2754 translation->z = mat.m14;
2755
2756 // Matrix Columns - Rotation will be extracted into here
2757 Vector3 matColumns[3] = {{ mat.m0, mat.m1, mat.m2 },
2758 { mat.m4, mat.m5, mat.m6 },
2759 { mat.m8, mat.m9, mat.m10 }};
2760
2761 // Shear Parameters XY, XZ, and YZ (extract and ignored)
2762 float shear[3] = { 0 };
2763
2764 // Normalized Scale Parameters
2765 Vector3 scl = { 0 };
2766
2767 // Max-Normalizing helps numerical stability
2768 float stabilizer = eps;
2769 for (int i = 0; i < 3; i++)
2770 {
2771 stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].x));
2772 stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].y));
2773 stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].z));
2774 }
2775 matColumns[0] = Vector3Scale(matColumns[0], 1.0f/stabilizer);
2776 matColumns[1] = Vector3Scale(matColumns[1], 1.0f/stabilizer);
2777 matColumns[2] = Vector3Scale(matColumns[2], 1.0f/stabilizer);
2778
2779 // X Scale
2780 scl.x = Vector3Length(matColumns[0]);
2781 if (scl.x > eps) matColumns[0] = Vector3Scale(matColumns[0], 1.0f/scl.x);
2782
2783 // Compute XY shear and make col2 orthogonal
2784 shear[0] = Vector3DotProduct(matColumns[0], matColumns[1]);
2785 matColumns[1] = Vector3Subtract(matColumns[1], Vector3Scale(matColumns[0], shear[0]));
2786
2787 // Y Scale
2788 scl.y = Vector3Length(matColumns[1]);
2789 if (scl.y > eps)
2790 {
2791 matColumns[1] = Vector3Scale(matColumns[1], 1.0f/scl.y);
2792 shear[0] /= scl.y; // Correct XY shear
2793 }
2794
2795 // Compute XZ and YZ shears and make col3 orthogonal
2796 shear[1] = Vector3DotProduct(matColumns[0], matColumns[2]);
2797 matColumns[2] = Vector3Subtract(matColumns[2], Vector3Scale(matColumns[0], shear[1]));
2798 shear[2] = Vector3DotProduct(matColumns[1], matColumns[2]);
2799 matColumns[2] = Vector3Subtract(matColumns[2], Vector3Scale(matColumns[1], shear[2]));
2800
2801 // Z Scale
2802 scl.z = Vector3Length(matColumns[2]);
2803 if (scl.z > eps)
2804 {
2805 matColumns[2] = Vector3Scale(matColumns[2], 1.0f/scl.z);
2806 shear[1] /= scl.z; // Correct XZ shear
2807 shear[2] /= scl.z; // Correct YZ shear
2808 }
2809
2810 // matColumns are now orthonormal in O(3). Now ensure its in SO(3) by enforcing det = 1
2811 if (Vector3DotProduct(matColumns[0], Vector3CrossProduct(matColumns[1], matColumns[2])) < 0)
2812 {
2813 scl = Vector3Negate(scl);
2814 matColumns[0] = Vector3Negate(matColumns[0]);
2815 matColumns[1] = Vector3Negate(matColumns[1]);
2816 matColumns[2] = Vector3Negate(matColumns[2]);
2817 }
2818
2819 // Set Scale
2820 *scale = Vector3Scale(scl, stabilizer);
2821
2822 // Extract Rotation
2823 Matrix rotationMatrix = { matColumns[0].x, matColumns[1].x, matColumns[2].x, 0,
2824 matColumns[0].y, matColumns[1].y, matColumns[2].y, 0,
2825 matColumns[0].z, matColumns[1].z, matColumns[2].z, 0,
2826 0, 0, 0, 1 };
2827 *rotation = QuaternionFromMatrix(rotationMatrix);
2828 }
2829
2830 #if defined(__cplusplus) && !defined(RAYMATH_DISABLE_CPP_OPERATORS)
2831
2832 // Optional C++ math operators
2833 //-------------------------------------------------------------------------------
2834
2835 // Vector2 operators
2836 static constexpr Vector2 Vector2Zeros = { 0, 0 };
2837 static constexpr Vector2 Vector2Ones = { 1, 1 };
2838 static constexpr Vector2 Vector2UnitX = { 1, 0 };
2839 static constexpr Vector2 Vector2UnitY = { 0, 1 };
2840
2841 inline Vector2 operator + (const Vector2& lhs, const Vector2& rhs)
2842 {
2843 return Vector2Add(lhs, rhs);
2844 }
2845
2846 inline const Vector2& operator += (Vector2& lhs, const Vector2& rhs)
2847 {
2848 lhs = Vector2Add(lhs, rhs);
2849 return lhs;
2850 }
2851
2852 inline Vector2 operator - (const Vector2& lhs, const Vector2& rhs)
2853 {
2854 return Vector2Subtract(lhs, rhs);
2855 }
2856
2857 inline const Vector2& operator -= (Vector2& lhs, const Vector2& rhs)
2858 {
2859 lhs = Vector2Subtract(lhs, rhs);
2860 return lhs;
2861 }
2862
2863 inline Vector2 operator * (const float& lhs, const Vector2& rhs)
2864 {
2865 return Vector2Scale(rhs, lhs);
2866 }
2867
2868 inline Vector2 operator * (const Vector2& lhs, const float& rhs)
2869 {
2870 return Vector2Scale(lhs, rhs);
2871 }
2872
2873 inline const Vector2& operator *= (Vector2& lhs, const float& rhs)
2874 {
2875 lhs = Vector2Scale(lhs, rhs);
2876 return lhs;
2877 }
2878
2879 inline Vector2 operator * (const Vector2& lhs, const Vector2& rhs)
2880 {
2881 return Vector2Multiply(lhs, rhs);
2882 }
2883
2884 inline const Vector2& operator *= (Vector2& lhs, const Vector2& rhs)
2885 {
2886 lhs = Vector2Multiply(lhs, rhs);
2887 return lhs;
2888 }
2889
2890 inline Vector2 operator * (const Vector2& lhs, const Matrix& rhs)
2891 {
2892 return Vector2Transform(lhs, rhs);
2893 }
2894
2895 inline const Vector2& operator *= (Vector2& lhs, const Matrix& rhs)
2896 {
2897 lhs = Vector2Transform(lhs, rhs);
2898 return lhs;
2899 }
2900
2901 inline Vector2 operator / (const Vector2& lhs, const float& rhs)
2902 {
2903 return Vector2Scale(lhs, 1.0f/rhs);
2904 }
2905
2906 inline const Vector2& operator /= (Vector2& lhs, const float& rhs)
2907 {
2908 lhs = Vector2Scale(lhs, 1.0f/rhs);
2909 return lhs;
2910 }
2911
2912 inline Vector2 operator / (const Vector2& lhs, const Vector2& rhs)
2913 {
2914 return Vector2Divide(lhs, rhs);
2915 }
2916
2917 inline const Vector2& operator /= (Vector2& lhs, const Vector2& rhs)
2918 {
2919 lhs = Vector2Divide(lhs, rhs);
2920 return lhs;
2921 }
2922
2923 inline bool operator == (const Vector2& lhs, const Vector2& rhs)
2924 {
2925 return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y);
2926 }
2927
2928 inline bool operator != (const Vector2& lhs, const Vector2& rhs)
2929 {
2930 return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y);
2931 }
2932
2933 // Vector3 operators
2934 static constexpr Vector3 Vector3Zeros = { 0, 0, 0 };
2935 static constexpr Vector3 Vector3Ones = { 1, 1, 1 };
2936 static constexpr Vector3 Vector3UnitX = { 1, 0, 0 };
2937 static constexpr Vector3 Vector3UnitY = { 0, 1, 0 };
2938 static constexpr Vector3 Vector3UnitZ = { 0, 0, 1 };
2939
2940 inline Vector3 operator + (const Vector3& lhs, const Vector3& rhs)
2941 {
2942 return Vector3Add(lhs, rhs);
2943 }
2944
2945 inline const Vector3& operator += (Vector3& lhs, const Vector3& rhs)
2946 {
2947 lhs = Vector3Add(lhs, rhs);
2948 return lhs;
2949 }
2950
2951 inline Vector3 operator - (const Vector3& lhs, const Vector3& rhs)
2952 {
2953 return Vector3Subtract(lhs, rhs);
2954 }
2955
2956 inline const Vector3& operator -= (Vector3& lhs, const Vector3& rhs)
2957 {
2958 lhs = Vector3Subtract(lhs, rhs);
2959 return lhs;
2960 }
2961
2962 inline Vector3 operator * (const float& lhs, const Vector3& rhs)
2963 {
2964 return Vector3Scale(rhs, lhs);
2965 }
2966
2967 inline Vector3 operator * (const Vector3& lhs, const float& rhs)
2968 {
2969 return Vector3Scale(lhs, rhs);
2970 }
2971
2972 inline const Vector3& operator *= (Vector3& lhs, const float& rhs)
2973 {
2974 lhs = Vector3Scale(lhs, rhs);
2975 return lhs;
2976 }
2977
2978 inline Vector3 operator * (const Vector3& lhs, const Vector3& rhs)
2979 {
2980 return Vector3Multiply(lhs, rhs);
2981 }
2982
2983 inline const Vector3& operator *= (Vector3& lhs, const Vector3& rhs)
2984 {
2985 lhs = Vector3Multiply(lhs, rhs);
2986 return lhs;
2987 }
2988
2989 inline Vector3 operator * (const Vector3& lhs, const Matrix& rhs)
2990 {
2991 return Vector3Transform(lhs, rhs);
2992 }
2993
2994 inline const Vector3& operator *= (Vector3& lhs, const Matrix& rhs)
2995 {
2996 lhs = Vector3Transform(lhs, rhs);
2997 return lhs;
2998 }
2999
3000 inline Vector3 operator / (const Vector3& lhs, const float& rhs)
3001 {
3002 return Vector3Scale(lhs, 1.0f/rhs);
3003 }
3004
3005 inline const Vector3& operator /= (Vector3& lhs, const float& rhs)
3006 {
3007 lhs = Vector3Scale(lhs, 1.0f/rhs);
3008 return lhs;
3009 }
3010
3011 inline Vector3 operator / (const Vector3& lhs, const Vector3& rhs)
3012 {
3013 return Vector3Divide(lhs, rhs);
3014 }
3015
3016 inline const Vector3& operator /= (Vector3& lhs, const Vector3& rhs)
3017 {
3018 lhs = Vector3Divide(lhs, rhs);
3019 return lhs;
3020 }
3021
3022 inline bool operator == (const Vector3& lhs, const Vector3& rhs)
3023 {
3024 return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y) && FloatEquals(lhs.z, rhs.z);
3025 }
3026
3027 inline bool operator != (const Vector3& lhs, const Vector3& rhs)
3028 {
3029 return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y) || !FloatEquals(lhs.z, rhs.z);
3030 }
3031
3032 // Vector4 operators
3033 static constexpr Vector4 Vector4Zeros = { 0, 0, 0, 0 };
3034 static constexpr Vector4 Vector4Ones = { 1, 1, 1, 1 };
3035 static constexpr Vector4 Vector4UnitX = { 1, 0, 0, 0 };
3036 static constexpr Vector4 Vector4UnitY = { 0, 1, 0, 0 };
3037 static constexpr Vector4 Vector4UnitZ = { 0, 0, 1, 0 };
3038 static constexpr Vector4 Vector4UnitW = { 0, 0, 0, 1 };
3039
3040 inline Vector4 operator + (const Vector4& lhs, const Vector4& rhs)
3041 {
3042 return Vector4Add(lhs, rhs);
3043 }
3044
3045 inline const Vector4& operator += (Vector4& lhs, const Vector4& rhs)
3046 {
3047 lhs = Vector4Add(lhs, rhs);
3048 return lhs;
3049 }
3050
3051 inline Vector4 operator - (const Vector4& lhs, const Vector4& rhs)
3052 {
3053 return Vector4Subtract(lhs, rhs);
3054 }
3055
3056 inline const Vector4& operator -= (Vector4& lhs, const Vector4& rhs)
3057 {
3058 lhs = Vector4Subtract(lhs, rhs);
3059 return lhs;
3060 }
3061
3062 inline Vector4 operator * (const float& lhs, const Vector4& rhs)
3063 {
3064 return Vector4Scale(rhs, lhs);
3065 }
3066
3067 inline Vector4 operator * (const Vector4& lhs, const float& rhs)
3068 {
3069 return Vector4Scale(lhs, rhs);
3070 }
3071
3072 inline const Vector4& operator *= (Vector4& lhs, const float& rhs)
3073 {
3074 lhs = Vector4Scale(lhs, rhs);
3075 return lhs;
3076 }
3077
3078 inline Vector4 operator * (const Vector4& lhs, const Vector4& rhs)
3079 {
3080 return Vector4Multiply(lhs, rhs);
3081 }
3082
3083 inline const Vector4& operator *= (Vector4& lhs, const Vector4& rhs)
3084 {
3085 lhs = Vector4Multiply(lhs, rhs);
3086 return lhs;
3087 }
3088
3089 inline Vector4 operator / (const Vector4& lhs, const float& rhs)
3090 {
3091 return Vector4Scale(lhs, 1.0f/rhs);
3092 }
3093
3094 inline const Vector4& operator /= (Vector4& lhs, const float& rhs)
3095 {
3096 lhs = Vector4Scale(lhs, 1.0f/rhs);
3097 return lhs;
3098 }
3099
3100 inline Vector4 operator / (const Vector4& lhs, const Vector4& rhs)
3101 {
3102 return Vector4Divide(lhs, rhs);
3103 }
3104
3105 inline const Vector4& operator /= (Vector4& lhs, const Vector4& rhs)
3106 {
3107 lhs = Vector4Divide(lhs, rhs);
3108 return lhs;
3109 }
3110
3111 inline bool operator == (const Vector4& lhs, const Vector4& rhs)
3112 {
3113 return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y) && FloatEquals(lhs.z, rhs.z) && FloatEquals(lhs.w, rhs.w);
3114 }
3115
3116 inline bool operator != (const Vector4& lhs, const Vector4& rhs)
3117 {
3118 return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y) || !FloatEquals(lhs.z, rhs.z) || !FloatEquals(lhs.w, rhs.w);
3119 }
3120
3121 // Quaternion operators
3122 static constexpr Quaternion QuaternionZeros = { 0, 0, 0, 0 };
3123 static constexpr Quaternion QuaternionOnes = { 1, 1, 1, 1 };
3124 static constexpr Quaternion QuaternionUnitX = { 0, 0, 0, 1 };
3125
3126 inline Quaternion operator + (const Quaternion& lhs, const float& rhs)
3127 {
3128 return QuaternionAddValue(lhs, rhs);
3129 }
3130
3131 inline const Quaternion& operator += (Quaternion& lhs, const float& rhs)
3132 {
3133 lhs = QuaternionAddValue(lhs, rhs);
3134 return lhs;
3135 }
3136
3137 inline Quaternion operator - (const Quaternion& lhs, const float& rhs)
3138 {
3139 return QuaternionSubtractValue(lhs, rhs);
3140 }
3141
3142 inline const Quaternion& operator -= (Quaternion& lhs, const float& rhs)
3143 {
3144 lhs = QuaternionSubtractValue(lhs, rhs);
3145 return lhs;
3146 }
3147
3148 inline Quaternion operator * (const Quaternion& lhs, const Matrix& rhs)
3149 {
3150 return QuaternionTransform(lhs, rhs);
3151 }
3152
3153 inline const Quaternion& operator *= (Quaternion& lhs, const Matrix& rhs)
3154 {
3155 lhs = QuaternionTransform(lhs, rhs);
3156 return lhs;
3157 }
3158
3159 // Matrix operators
3160 static constexpr Matrix MatrixUnit = { 1, 0, 0, 0,
3161 0, 1, 0, 0,
3162 0, 0, 1, 0,
3163 0, 0, 0, 1 };
3164
3165 inline Matrix operator + (const Matrix& lhs, const Matrix& rhs)
3166 {
3167 return MatrixAdd(lhs, rhs);
3168 }
3169
3170 inline const Matrix& operator += (Matrix& lhs, const Matrix& rhs)
3171 {
3172 lhs = MatrixAdd(lhs, rhs);
3173 return lhs;
3174 }
3175
3176 inline Matrix operator - (const Matrix& lhs, const Matrix& rhs)
3177 {
3178 return MatrixSubtract(lhs, rhs);
3179 }
3180
3181 inline const Matrix& operator -= (Matrix& lhs, const Matrix& rhs)
3182 {
3183 lhs = MatrixSubtract(lhs, rhs);
3184 return lhs;
3185 }
3186
3187 inline Matrix operator * (const Matrix& lhs, const Matrix& rhs)
3188 {
3189 return MatrixMultiply(lhs, rhs);
3190 }
3191
3192 inline const Matrix& operator *= (Matrix& lhs, const Matrix& rhs)
3193 {
3194 lhs = MatrixMultiply(lhs, rhs);
3195 return lhs;
3196 }
3197
3198 inline Matrix operator * (const Matrix& lhs, const float value)
3199 {
3200 return MatrixMultiplyValue(lhs, value);
3201 }
3202
3203 inline const Matrix& operator *= (Matrix& lhs, const float value)
3204 {
3205 lhs = MatrixMultiplyValue(lhs, value);
3206 return lhs;
3207 }
3208
3209 //-------------------------------------------------------------------------------
3210 #endif // C++ operators
3211
3212 #endif // RAYMATH_H