55 #if defined(RAYMATH_IMPLEMENTATION) && defined(RAYMATH_STATIC_INLINE) 56 #error "Specifying both RAYMATH_IMPLEMENTATION and RAYMATH_STATIC_INLINE is contradictory" 60 #if defined(RAYMATH_IMPLEMENTATION) 61 #if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED) 62 #define RMAPI __declspec(dllexport) extern inline // We are building raylib as a Win32 shared library (.dll) 63 #elif defined(BUILD_LIBTYPE_SHARED) 64 #define RMAPI __attribute__((visibility("default"))) // We are building raylib as a Unix shared library (.so/.dylib) 65 #elif defined(_WIN32) && defined(USE_LIBTYPE_SHARED) 66 #define RMAPI __declspec(dllimport) // We are using raylib as a Win32 shared library (.dll) 68 #define RMAPI extern inline // Provide external definition 70 #elif defined(RAYMATH_STATIC_INLINE) 71 #define RMAPI static inline // Functions may be inlined, no external out-of-line definition 73 #if defined(__TINYC__) 74 #define RMAPI static inline // plain inline not supported by tinycc (See issue #435) 76 #define RMAPI inline // Functions may be inlined or external definition used 85 #define PI 3.14159265358979323846f 89 #define EPSILON 0.000001f 93 #define DEG2RAD (PI/180.0f) 97 #define RAD2DEG (180.0f/PI) 101 #ifndef MatrixToFloat 102 #define MatrixToFloat(mat) (MatrixToFloatV(mat).v) 106 #ifndef Vector3ToFloat 107 #define Vector3ToFloat(vec) (Vector3ToFloatV(vec).v) 113 #if !defined(RL_VECTOR2_TYPE) 119 #define RL_VECTOR2_TYPE 122 #if !defined(RL_VECTOR3_TYPE) 129 #define RL_VECTOR3_TYPE 132 #if !defined(RL_VECTOR4_TYPE) 140 #define RL_VECTOR4_TYPE 143 #if !defined(RL_QUATERNION_TYPE) 146 #define RL_QUATERNION_TYPE 149 #if !defined(RL_MATRIX_TYPE) 152 float m0, m4, m8, m12;
153 float m1, m5, m9, m13;
154 float m2, m6, m10, m14;
155 float m3, m7, m11, m15;
157 #define RL_MATRIX_TYPE 176 RMAPI
float Clamp(
float value,
float min,
float max)
178 float result = (value < min) ? min : value;
180 if (result > max) result = max;
186 RMAPI
float Lerp(
float start,
float end,
float amount)
188 float result = start + amount*(end - start);
194 RMAPI
float Normalize(
float value,
float start,
float end)
196 float result = (value - start)/(end - start);
202 RMAPI
float Remap(
float value,
float inputStart,
float inputEnd,
float outputStart,
float outputEnd)
204 float result = (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;
210 RMAPI
float Wrap(
float value,
float min,
float max)
212 float result = value - (max - min)*floorf((value - min)/(max - min));
218 RMAPI
int FloatEquals(
float x,
float y)
220 #if !defined(EPSILON) 221 #define EPSILON 0.000001f 224 int result = (fabsf(x - y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))));
234 RMAPI
Vector2 Vector2Zero(
void)
236 Vector2 result = { 0.0f, 0.0f };
244 Vector2 result = { 1.0f, 1.0f };
252 Vector2 result = { v1.x + v2.x, v1.y + v2.y };
260 Vector2 result = { v.x + add, v.y + add };
268 Vector2 result = { v1.x - v2.x, v1.y - v2.y };
276 Vector2 result = { v.x - sub, v.y - sub };
282 RMAPI
float Vector2Length(
Vector2 v)
284 float result = sqrtf((v.x*v.x) + (v.y*v.y));
290 RMAPI
float Vector2LengthSqr(
Vector2 v)
292 float result = (v.x*v.x) + (v.y*v.y);
300 float result = (v1.x*v2.x + v1.y*v2.y);
308 float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
316 float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
327 float dot = v1.x*v2.x + v1.y*v2.y;
328 float det = v1.x*v2.y - v1.y*v2.x;
330 result = atan2f(det, dot);
343 result = -atan2f(end.y - start.y, end.x - start.x);
351 Vector2 result = { v.x*scale, v.y*scale };
359 Vector2 result = { v1.x*v2.x, v1.y*v2.y };
367 Vector2 result = { -v.x, -v.y };
375 Vector2 result = { v1.x/v2.x, v1.y/v2.y };
384 float length = sqrtf((v.x*v.x) + (v.y*v.y));
388 float ilength = 1.0f/length;
389 result.x = v.x*ilength;
390 result.y = v.y*ilength;
405 result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
406 result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
416 result.x = v1.x + amount*(v2.x - v1.x);
417 result.y = v1.y + amount*(v2.y - v1.y);
427 float dotProduct = (v.x*normal.x + v.y*normal.y);
429 result.x = v.x - (2.0f*normal.x)*dotProduct;
430 result.y = v.y - (2.0f*normal.y)*dotProduct;
440 result.x = fminf(v1.x, v2.x);
441 result.y = fminf(v1.y, v2.y);
451 result.x = fmaxf(v1.x, v2.x);
452 result.y = fmaxf(v1.y, v2.y);
462 float cosres = cosf(angle);
463 float sinres = sinf(angle);
465 result.x = v.x*cosres - v.y*sinres;
466 result.y = v.x*sinres + v.y*cosres;
476 float dx = target.x - v.x;
477 float dy = target.y - v.y;
478 float value = (dx*dx) + (dy*dy);
480 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance)))
return target;
482 float dist = sqrtf(value);
484 result.x = v.x + dx/dist*maxDistance;
485 result.y = v.y + dy/dist*maxDistance;
493 Vector2 result = { 1.0f/v.x, 1.0f/v.y };
504 result.x = fminf(max.x, fmaxf(min.x, v.x));
505 result.y = fminf(max.y, fmaxf(min.y, v.y));
515 float length = (v.x*v.x) + (v.y*v.y);
518 length = sqrtf(length);
525 else if (length > max)
530 result.x = v.x*scale;
531 result.y = v.y*scale;
540 #if !defined(EPSILON) 541 #define EPSILON 0.000001f 544 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
545 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))));
559 float dot = v.x*n.x + v.y*n.y;
560 float d = 1.0f - r*r*(1.0f - dot*dot);
565 v.x = r*v.x - (r*dot + d)*n.x;
566 v.y = r*v.y - (r*dot + d)*n.y;
580 RMAPI
Vector3 Vector3Zero(
void)
582 Vector3 result = { 0.0f, 0.0f, 0.0f };
590 Vector3 result = { 1.0f, 1.0f, 1.0f };
598 Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
606 Vector3 result = { v.x + add, v.y + add, v.z + add };
614 Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
622 Vector3 result = { v.x - sub, v.y - sub, v.z - sub };
630 Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };
638 Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };
646 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 };
656 float min = fabsf(v.x);
657 Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
659 if (fabsf(v.y) < min)
662 Vector3 tmp = {0.0f, 1.0f, 0.0f};
666 if (fabsf(v.z) < min)
668 Vector3 tmp = {0.0f, 0.0f, 1.0f};
673 result.x = v.y*cardinalAxis.z - v.z*cardinalAxis.y;
674 result.y = v.z*cardinalAxis.x - v.x*cardinalAxis.z;
675 result.z = v.x*cardinalAxis.y - v.y*cardinalAxis.x;
681 RMAPI
float Vector3Length(
const Vector3 v)
683 float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
689 RMAPI
float Vector3LengthSqr(
const Vector3 v)
691 float result = v.x*v.x + v.y*v.y + v.z*v.z;
699 float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
709 float dx = v2.x - v1.x;
710 float dy = v2.y - v1.y;
711 float dz = v2.z - v1.z;
712 result = sqrtf(dx*dx + dy*dy + dz*dz);
722 float dx = v2.x - v1.x;
723 float dy = v2.y - v1.y;
724 float dz = v2.z - v1.z;
725 result = dx*dx + dy*dy + dz*dz;
735 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 };
736 float len = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z);
737 float dot = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
738 result = atan2f(len, dot);
746 Vector3 result = { -v.x, -v.y, -v.z };
754 Vector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };
764 float length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
767 float ilength = 1.0f/length;
782 float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
783 float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
785 float mag = v1dv2/v2dv2;
799 float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
800 float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
802 float mag = v1dv2/v2dv2;
804 result.x = v1.x - (v2.x*mag);
805 result.y = v1.y - (v2.y*mag);
806 result.z = v1.z - (v2.z*mag);
817 float ilength = 0.0f;
821 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
822 if (length == 0.0f) length = 1.0f;
823 ilength = 1.0f/length;
829 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 };
833 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
834 if (length == 0.0f) length = 1.0f;
835 ilength = 1.0f/length;
841 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 };
855 result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
856 result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
857 result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;
867 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);
868 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);
869 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);
883 float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
884 if (length == 0.0f) length = 1.0f;
885 float ilength = 1.0f/length;
891 float a = sinf(angle);
899 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 };
902 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 };
931 float dx = target.x - v.x;
932 float dy = target.y - v.y;
933 float dz = target.z - v.z;
934 float value = (dx*dx) + (dy*dy) + (dz*dz);
936 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance)))
return target;
938 float dist = sqrtf(value);
940 result.x = v.x + dx/dist*maxDistance;
941 result.y = v.y + dy/dist*maxDistance;
942 result.z = v.z + dz/dist*maxDistance;
952 result.x = v1.x + amount*(v2.x - v1.x);
953 result.y = v1.y + amount*(v2.y - v1.y);
954 result.z = v1.z + amount*(v2.z - v1.z);
965 float amountPow2 = amount * amount;
966 float amountPow3 = amount * amount * amount;
968 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;
969 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;
970 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;
984 float dotProduct = (v.x*normal.x + v.y*normal.y + v.z*normal.z);
986 result.x = v.x - (2.0f*normal.x)*dotProduct;
987 result.y = v.y - (2.0f*normal.y)*dotProduct;
988 result.z = v.z - (2.0f*normal.z)*dotProduct;
998 result.x = fminf(v1.x, v2.x);
999 result.y = fminf(v1.y, v2.y);
1000 result.z = fminf(v1.z, v2.z);
1010 result.x = fmaxf(v1.x, v2.x);
1011 result.y = fmaxf(v1.y, v2.y);
1012 result.z = fmaxf(v1.z, v2.z);
1023 Vector3 v0 = { b.x - a.x, b.y - a.y, b.z - a.z };
1024 Vector3 v1 = { c.x - a.x, c.y - a.y, c.z - a.z };
1025 Vector3 v2 = { p.x - a.x, p.y - a.y, p.z - a.z };
1026 float d00 = (v0.x*v0.x + v0.y*v0.y + v0.z*v0.z);
1027 float d01 = (v0.x*v1.x + v0.y*v1.y + v0.z*v1.z);
1028 float d11 = (v1.x*v1.x + v1.y*v1.y + v1.z*v1.z);
1029 float d20 = (v2.x*v0.x + v2.y*v0.y + v2.z*v0.z);
1030 float d21 = (v2.x*v1.x + v2.y*v1.y + v2.z*v1.z);
1032 float denom = d00*d11 - d01*d01;
1034 result.y = (d11*d20 - d01*d21)/denom;
1035 result.z = (d00*d21 - d01*d20)/denom;
1036 result.x = 1.0f - (result.z + result.y);
1049 view.m0*projection.m0 + view.m1*projection.m4 + view.m2*projection.m8 + view.m3*projection.m12,
1050 view.m0*projection.m1 + view.m1*projection.m5 + view.m2*projection.m9 + view.m3*projection.m13,
1051 view.m0*projection.m2 + view.m1*projection.m6 + view.m2*projection.m10 + view.m3*projection.m14,
1052 view.m0*projection.m3 + view.m1*projection.m7 + view.m2*projection.m11 + view.m3*projection.m15,
1053 view.m4*projection.m0 + view.m5*projection.m4 + view.m6*projection.m8 + view.m7*projection.m12,
1054 view.m4*projection.m1 + view.m5*projection.m5 + view.m6*projection.m9 + view.m7*projection.m13,
1055 view.m4*projection.m2 + view.m5*projection.m6 + view.m6*projection.m10 + view.m7*projection.m14,
1056 view.m4*projection.m3 + view.m5*projection.m7 + view.m6*projection.m11 + view.m7*projection.m15,
1057 view.m8*projection.m0 + view.m9*projection.m4 + view.m10*projection.m8 + view.m11*projection.m12,
1058 view.m8*projection.m1 + view.m9*projection.m5 + view.m10*projection.m9 + view.m11*projection.m13,
1059 view.m8*projection.m2 + view.m9*projection.m6 + view.m10*projection.m10 + view.m11*projection.m14,
1060 view.m8*projection.m3 + view.m9*projection.m7 + view.m10*projection.m11 + view.m11*projection.m15,
1061 view.m12*projection.m0 + view.m13*projection.m4 + view.m14*projection.m8 + view.m15*projection.m12,
1062 view.m12*projection.m1 + view.m13*projection.m5 + view.m14*projection.m9 + view.m15*projection.m13,
1063 view.m12*projection.m2 + view.m13*projection.m6 + view.m14*projection.m10 + view.m15*projection.m14,
1064 view.m12*projection.m3 + view.m13*projection.m7 + view.m14*projection.m11 + view.m15*projection.m15 };
1068 float a00 = matViewProj.m0, a01 = matViewProj.m1, a02 = matViewProj.m2, a03 = matViewProj.m3;
1069 float a10 = matViewProj.m4, a11 = matViewProj.m5, a12 = matViewProj.m6, a13 = matViewProj.m7;
1070 float a20 = matViewProj.m8, a21 = matViewProj.m9, a22 = matViewProj.m10, a23 = matViewProj.m11;
1071 float a30 = matViewProj.m12, a31 = matViewProj.m13, a32 = matViewProj.m14, a33 = matViewProj.m15;
1073 float b00 = a00*a11 - a01*a10;
1074 float b01 = a00*a12 - a02*a10;
1075 float b02 = a00*a13 - a03*a10;
1076 float b03 = a01*a12 - a02*a11;
1077 float b04 = a01*a13 - a03*a11;
1078 float b05 = a02*a13 - a03*a12;
1079 float b06 = a20*a31 - a21*a30;
1080 float b07 = a20*a32 - a22*a30;
1081 float b08 = a20*a33 - a23*a30;
1082 float b09 = a21*a32 - a22*a31;
1083 float b10 = a21*a33 - a23*a31;
1084 float b11 = a22*a33 - a23*a32;
1087 float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
1089 Matrix matViewProjInv = {
1090 (a11*b11 - a12*b10 + a13*b09)*invDet,
1091 (-a01*b11 + a02*b10 - a03*b09)*invDet,
1092 (a31*b05 - a32*b04 + a33*b03)*invDet,
1093 (-a21*b05 + a22*b04 - a23*b03)*invDet,
1094 (-a10*b11 + a12*b08 - a13*b07)*invDet,
1095 (a00*b11 - a02*b08 + a03*b07)*invDet,
1096 (-a30*b05 + a32*b02 - a33*b01)*invDet,
1097 (a20*b05 - a22*b02 + a23*b01)*invDet,
1098 (a10*b10 - a11*b08 + a13*b06)*invDet,
1099 (-a00*b10 + a01*b08 - a03*b06)*invDet,
1100 (a30*b04 - a31*b02 + a33*b00)*invDet,
1101 (-a20*b04 + a21*b02 - a23*b00)*invDet,
1102 (-a10*b09 + a11*b07 - a12*b06)*invDet,
1103 (a00*b09 - a01*b07 + a02*b06)*invDet,
1104 (-a30*b03 + a31*b01 - a32*b00)*invDet,
1105 (a20*b03 - a21*b01 + a22*b00)*invDet };
1108 Quaternion quat = { source.x, source.y, source.z, 1.0f };
1112 matViewProjInv.m0*quat.x + matViewProjInv.m4*quat.y + matViewProjInv.m8*quat.z + matViewProjInv.m12*quat.w,
1113 matViewProjInv.m1*quat.x + matViewProjInv.m5*quat.y + matViewProjInv.m9*quat.z + matViewProjInv.m13*quat.w,
1114 matViewProjInv.m2*quat.x + matViewProjInv.m6*quat.y + matViewProjInv.m10*quat.z + matViewProjInv.m14*quat.w,
1115 matViewProjInv.m3*quat.x + matViewProjInv.m7*quat.y + matViewProjInv.m11*quat.z + matViewProjInv.m15*quat.w };
1118 result.x = qtransformed.x/qtransformed.w;
1119 result.y = qtransformed.y/qtransformed.w;
1120 result.z = qtransformed.z/qtransformed.w;
1140 Vector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };
1151 result.x = fminf(max.x, fmaxf(min.x, v.x));
1152 result.y = fminf(max.y, fmaxf(min.y, v.y));
1153 result.z = fminf(max.z, fmaxf(min.z, v.z));
1163 float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
1166 length = sqrtf(length);
1173 else if (length > max)
1178 result.x = v.x*scale;
1179 result.y = v.y*scale;
1180 result.z = v.z*scale;
1189 #if !defined(EPSILON) 1190 #define EPSILON 0.000001f 1193 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
1194 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
1195 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z)))));
1209 float dot = v.x*n.x + v.y*n.y + v.z*n.z;
1210 float d = 1.0f - r*r*(1.0f - dot*dot);
1215 v.x = r*v.x - (r*dot + d)*n.x;
1216 v.y = r*v.y - (r*dot + d)*n.y;
1217 v.z = r*v.z - (r*dot + d)*n.z;
1230 RMAPI
Vector4 Vector4Zero(
void)
1232 Vector4 result = { 0.0f, 0.0f, 0.0f, 0.0f };
1236 RMAPI
Vector4 Vector4One(
void)
1238 Vector4 result = { 1.0f, 1.0f, 1.0f, 1.0f };
1286 RMAPI
float Vector4Length(
Vector4 v)
1288 float result = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
1292 RMAPI
float Vector4LengthSqr(
Vector4 v)
1294 float result = (v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w);
1300 float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w);
1307 float result = sqrtf(
1308 (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
1309 (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w));
1317 (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
1318 (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w);
1325 Vector4 result = { v.x*scale, v.y*scale, v.z*scale, v.w*scale };
1332 Vector4 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z, v1.w*v2.w };
1339 Vector4 result = { -v.x, -v.y, -v.z, -v.w };
1346 Vector4 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z, v1.w/v2.w };
1354 float length = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
1358 float ilength = 1.0f/length;
1359 result.x = v.x*ilength;
1360 result.y = v.y*ilength;
1361 result.z = v.z*ilength;
1362 result.w = v.w*ilength;
1373 result.x = fminf(v1.x, v2.x);
1374 result.y = fminf(v1.y, v2.y);
1375 result.z = fminf(v1.z, v2.z);
1376 result.w = fminf(v1.w, v2.w);
1386 result.x = fmaxf(v1.x, v2.x);
1387 result.y = fmaxf(v1.y, v2.y);
1388 result.z = fmaxf(v1.z, v2.z);
1389 result.w = fmaxf(v1.w, v2.w);
1399 result.x = v1.x + amount*(v2.x - v1.x);
1400 result.y = v1.y + amount*(v2.y - v1.y);
1401 result.z = v1.z + amount*(v2.z - v1.z);
1402 result.w = v1.w + amount*(v2.w - v1.w);
1412 float dx = target.x - v.x;
1413 float dy = target.y - v.y;
1414 float dz = target.z - v.z;
1415 float dw = target.w - v.w;
1416 float value = (dx*dx) + (dy*dy) + (dz*dz) + (dw*dw);
1418 if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance)))
return target;
1420 float dist = sqrtf(value);
1422 result.x = v.x + dx/dist*maxDistance;
1423 result.y = v.y + dy/dist*maxDistance;
1424 result.z = v.z + dz/dist*maxDistance;
1425 result.w = v.w + dw/dist*maxDistance;
1433 Vector4 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z, 1.0f/v.w };
1440 #if !defined(EPSILON) 1441 #define EPSILON 0.000001f 1444 int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
1445 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
1446 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
1447 ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))));
1457 RMAPI
float MatrixDeterminant(
Matrix mat)
1459 float result = 0.0f;
1462 float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
1463 float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
1464 float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
1465 float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
1467 result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
1468 a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
1469 a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
1470 a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
1471 a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
1472 a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
1478 RMAPI
float MatrixTrace(
Matrix mat)
1480 float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);
1493 result.m3 = mat.m12;
1497 result.m7 = mat.m13;
1500 result.m10 = mat.m10;
1501 result.m11 = mat.m14;
1502 result.m12 = mat.m3;
1503 result.m13 = mat.m7;
1504 result.m14 = mat.m11;
1505 result.m15 = mat.m15;
1516 float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
1517 float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
1518 float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
1519 float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
1521 float b00 = a00*a11 - a01*a10;
1522 float b01 = a00*a12 - a02*a10;
1523 float b02 = a00*a13 - a03*a10;
1524 float b03 = a01*a12 - a02*a11;
1525 float b04 = a01*a13 - a03*a11;
1526 float b05 = a02*a13 - a03*a12;
1527 float b06 = a20*a31 - a21*a30;
1528 float b07 = a20*a32 - a22*a30;
1529 float b08 = a20*a33 - a23*a30;
1530 float b09 = a21*a32 - a22*a31;
1531 float b10 = a21*a33 - a23*a31;
1532 float b11 = a22*a33 - a23*a32;
1535 float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
1537 result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
1538 result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
1539 result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
1540 result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
1541 result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
1542 result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
1543 result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
1544 result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
1545 result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
1546 result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
1547 result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
1548 result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
1549 result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
1550 result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
1551 result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
1552 result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;
1558 RMAPI
Matrix MatrixIdentity(
void)
1560 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1561 0.0f, 1.0f, 0.0f, 0.0f,
1562 0.0f, 0.0f, 1.0f, 0.0f,
1563 0.0f, 0.0f, 0.0f, 1.0f };
1573 result.m0 = left.m0 + right.m0;
1574 result.m1 = left.m1 + right.m1;
1575 result.m2 = left.m2 + right.m2;
1576 result.m3 = left.m3 + right.m3;
1577 result.m4 = left.m4 + right.m4;
1578 result.m5 = left.m5 + right.m5;
1579 result.m6 = left.m6 + right.m6;
1580 result.m7 = left.m7 + right.m7;
1581 result.m8 = left.m8 + right.m8;
1582 result.m9 = left.m9 + right.m9;
1583 result.m10 = left.m10 + right.m10;
1584 result.m11 = left.m11 + right.m11;
1585 result.m12 = left.m12 + right.m12;
1586 result.m13 = left.m13 + right.m13;
1587 result.m14 = left.m14 + right.m14;
1588 result.m15 = left.m15 + right.m15;
1598 result.m0 = left.m0 - right.m0;
1599 result.m1 = left.m1 - right.m1;
1600 result.m2 = left.m2 - right.m2;
1601 result.m3 = left.m3 - right.m3;
1602 result.m4 = left.m4 - right.m4;
1603 result.m5 = left.m5 - right.m5;
1604 result.m6 = left.m6 - right.m6;
1605 result.m7 = left.m7 - right.m7;
1606 result.m8 = left.m8 - right.m8;
1607 result.m9 = left.m9 - right.m9;
1608 result.m10 = left.m10 - right.m10;
1609 result.m11 = left.m11 - right.m11;
1610 result.m12 = left.m12 - right.m12;
1611 result.m13 = left.m13 - right.m13;
1612 result.m14 = left.m14 - right.m14;
1613 result.m15 = left.m15 - right.m15;
1624 result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
1625 result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
1626 result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
1627 result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
1628 result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
1629 result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
1630 result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
1631 result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
1632 result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
1633 result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
1634 result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
1635 result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
1636 result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
1637 result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
1638 result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
1639 result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
1645 RMAPI
Matrix MatrixTranslate(
float x,
float y,
float z)
1647 Matrix result = { 1.0f, 0.0f, 0.0f, x,
1648 0.0f, 1.0f, 0.0f, y,
1649 0.0f, 0.0f, 1.0f, z,
1650 0.0f, 0.0f, 0.0f, 1.0f };
1661 float x = axis.x, y = axis.y, z = axis.z;
1663 float lengthSquared = x*x + y*y + z*z;
1665 if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
1667 float ilength = 1.0f/sqrtf(lengthSquared);
1673 float sinres = sinf(angle);
1674 float cosres = cosf(angle);
1675 float t = 1.0f - cosres;
1677 result.m0 = x*x*t + cosres;
1678 result.m1 = y*x*t + z*sinres;
1679 result.m2 = z*x*t - y*sinres;
1682 result.m4 = x*y*t - z*sinres;
1683 result.m5 = y*y*t + cosres;
1684 result.m6 = z*y*t + x*sinres;
1687 result.m8 = x*z*t + y*sinres;
1688 result.m9 = y*z*t - x*sinres;
1689 result.m10 = z*z*t + cosres;
1702 RMAPI
Matrix MatrixRotateX(
float angle)
1704 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1705 0.0f, 1.0f, 0.0f, 0.0f,
1706 0.0f, 0.0f, 1.0f, 0.0f,
1707 0.0f, 0.0f, 0.0f, 1.0f };
1709 float cosres = cosf(angle);
1710 float sinres = sinf(angle);
1714 result.m9 = -sinres;
1715 result.m10 = cosres;
1722 RMAPI
Matrix MatrixRotateY(
float angle)
1724 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1725 0.0f, 1.0f, 0.0f, 0.0f,
1726 0.0f, 0.0f, 1.0f, 0.0f,
1727 0.0f, 0.0f, 0.0f, 1.0f };
1729 float cosres = cosf(angle);
1730 float sinres = sinf(angle);
1733 result.m2 = -sinres;
1735 result.m10 = cosres;
1742 RMAPI
Matrix MatrixRotateZ(
float angle)
1744 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1745 0.0f, 1.0f, 0.0f, 0.0f,
1746 0.0f, 0.0f, 1.0f, 0.0f,
1747 0.0f, 0.0f, 0.0f, 1.0f };
1749 float cosres = cosf(angle);
1750 float sinres = sinf(angle);
1754 result.m4 = -sinres;
1765 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
1766 0.0f, 1.0f, 0.0f, 0.0f,
1767 0.0f, 0.0f, 1.0f, 0.0f,
1768 0.0f, 0.0f, 0.0f, 1.0f };
1770 float cosz = cosf(-angle.z);
1771 float sinz = sinf(-angle.z);
1772 float cosy = cosf(-angle.y);
1773 float siny = sinf(-angle.y);
1774 float cosx = cosf(-angle.x);
1775 float sinx = sinf(-angle.x);
1777 result.m0 = cosz*cosy;
1778 result.m1 = (cosz*siny*sinx) - (sinz*cosx);
1779 result.m2 = (cosz*siny*cosx) + (sinz*sinx);
1781 result.m4 = sinz*cosy;
1782 result.m5 = (sinz*siny*sinx) + (cosz*cosx);
1783 result.m6 = (sinz*siny*cosx) - (cosz*sinx);
1786 result.m9 = cosy*sinx;
1787 result.m10= cosy*cosx;
1798 float cz = cosf(angle.z);
1799 float sz = sinf(angle.z);
1800 float cy = cosf(angle.y);
1801 float sy = sinf(angle.y);
1802 float cx = cosf(angle.x);
1803 float sx = sinf(angle.x);
1806 result.m4 = cz*sy*sx - cx*sz;
1807 result.m8 = sz*sx + cz*cx*sy;
1811 result.m5 = cz*cx + sz*sy*sx;
1812 result.m9 = cx*sz*sy - cz*sx;
1829 RMAPI
Matrix MatrixScale(
float x,
float y,
float z)
1831 Matrix result = { x, 0.0f, 0.0f, 0.0f,
1832 0.0f, y, 0.0f, 0.0f,
1833 0.0f, 0.0f, z, 0.0f,
1834 0.0f, 0.0f, 0.0f, 1.0f };
1840 RMAPI
Matrix MatrixFrustum(
double left,
double right,
double bottom,
double top,
double near,
double far)
1844 float rl = (float)(right - left);
1845 float tb = (float)(top - bottom);
1846 float fn = (float)(far - near);
1848 result.m0 = ((float)near*2.0f)/rl;
1854 result.m5 = ((float)near*2.0f)/tb;
1858 result.m8 = ((float)right + (
float)left)/rl;
1859 result.m9 = ((float)top + (
float)bottom)/tb;
1860 result.m10 = -((float)far + (
float)near)/fn;
1865 result.m14 = -((float)far*(
float)near*2.0f)/fn;
1873 RMAPI
Matrix MatrixPerspective(
double fovY,
double aspect,
double nearPlane,
double farPlane)
1877 double top = nearPlane*tan(fovY*0.5);
1878 double bottom = -top;
1879 double right = top*aspect;
1880 double left = -right;
1883 float rl = (float)(right - left);
1884 float tb = (float)(top - bottom);
1885 float fn = (float)(farPlane - nearPlane);
1887 result.m0 = ((float)nearPlane*2.0f)/rl;
1888 result.m5 = ((float)nearPlane*2.0f)/tb;
1889 result.m8 = ((float)right + (
float)left)/rl;
1890 result.m9 = ((float)top + (
float)bottom)/tb;
1891 result.m10 = -((float)farPlane + (
float)nearPlane)/fn;
1893 result.m14 = -((float)farPlane*(
float)nearPlane*2.0f)/fn;
1899 RMAPI
Matrix MatrixOrtho(
double left,
double right,
double bottom,
double top,
double nearPlane,
double farPlane)
1903 float rl = (float)(right - left);
1904 float tb = (float)(top - bottom);
1905 float fn = (float)(farPlane - nearPlane);
1907 result.m0 = 2.0f/rl;
1912 result.m5 = 2.0f/tb;
1917 result.m10 = -2.0f/fn;
1919 result.m12 = -((float)left + (
float)right)/rl;
1920 result.m13 = -((float)top + (
float)bottom)/tb;
1921 result.m14 = -((float)farPlane + (
float)nearPlane)/fn;
1932 float length = 0.0f;
1933 float ilength = 0.0f;
1936 Vector3 vz = { eye.x - target.x, eye.y - target.y, eye.z - target.z };
1940 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
1941 if (length == 0.0f) length = 1.0f;
1942 ilength = 1.0f/length;
1948 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 };
1952 length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
1953 if (length == 0.0f) length = 1.0f;
1954 ilength = 1.0f/length;
1960 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 };
1974 result.m12 = -(vx.x*eye.x + vx.y*eye.y + vx.z*eye.z);
1975 result.m13 = -(vy.x*eye.x + vy.y*eye.y + vy.z*eye.z);
1976 result.m14 = -(vz.x*eye.x + vz.y*eye.y + vz.z*eye.z);
1987 result.v[0] = mat.m0;
1988 result.v[1] = mat.m1;
1989 result.v[2] = mat.m2;
1990 result.v[3] = mat.m3;
1991 result.v[4] = mat.m4;
1992 result.v[5] = mat.m5;
1993 result.v[6] = mat.m6;
1994 result.v[7] = mat.m7;
1995 result.v[8] = mat.m8;
1996 result.v[9] = mat.m9;
1997 result.v[10] = mat.m10;
1998 result.v[11] = mat.m11;
1999 result.v[12] = mat.m12;
2000 result.v[13] = mat.m13;
2001 result.v[14] = mat.m14;
2002 result.v[15] = mat.m15;
2014 Quaternion result = {q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w};
2022 Quaternion result = {q.x + add, q.y + add, q.z + add, q.w + add};
2030 Quaternion result = {q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w};
2038 Quaternion result = {q.x - sub, q.y - sub, q.z - sub, q.w - sub};
2046 Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
2054 float result = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2064 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2065 if (length == 0.0f) length = 1.0f;
2066 float ilength = 1.0f/length;
2068 result.x = q.x*ilength;
2069 result.y = q.y*ilength;
2070 result.z = q.z*ilength;
2071 result.w = q.w*ilength;
2081 float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
2083 if (lengthSq != 0.0f)
2085 float invLength = 1.0f/lengthSq;
2087 result.x *= -invLength;
2088 result.y *= -invLength;
2089 result.z *= -invLength;
2090 result.w *= invLength;
2101 float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w;
2102 float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w;
2104 result.x = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
2105 result.y = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
2106 result.z = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
2107 result.w = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;
2128 Quaternion result = { q1.x/q2.x, q1.y/q2.y, q1.z/q2.z, q1.w/q2.w };
2138 result.x = q1.x + amount*(q2.x - q1.x);
2139 result.y = q1.y + amount*(q2.y - q1.y);
2140 result.z = q1.z + amount*(q2.z - q1.z);
2141 result.w = q1.w + amount*(q2.w - q1.w);
2152 result.x = q1.x + amount*(q2.x - q1.x);
2153 result.y = q1.y + amount*(q2.y - q1.y);
2154 result.z = q1.z + amount*(q2.z - q1.z);
2155 result.w = q1.w + amount*(q2.w - q1.w);
2159 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2160 if (length == 0.0f) length = 1.0f;
2161 float ilength = 1.0f/length;
2163 result.x = q.x*ilength;
2164 result.y = q.y*ilength;
2165 result.z = q.z*ilength;
2166 result.w = q.w*ilength;
2176 #if !defined(EPSILON) 2177 #define EPSILON 0.000001f 2180 float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
2182 if (cosHalfTheta < 0)
2184 q2.x = -q2.x; q2.y = -q2.y; q2.z = -q2.z; q2.w = -q2.w;
2185 cosHalfTheta = -cosHalfTheta;
2188 if (fabsf(cosHalfTheta) >= 1.0f) result = q1;
2189 else if (cosHalfTheta > 0.95f) result = QuaternionNlerp(q1, q2, amount);
2192 float halfTheta = acosf(cosHalfTheta);
2193 float sinHalfTheta = sqrtf(1.0f - cosHalfTheta*cosHalfTheta);
2195 if (fabsf(sinHalfTheta) < EPSILON)
2197 result.x = (q1.x*0.5f + q2.x*0.5f);
2198 result.y = (q1.y*0.5f + q2.y*0.5f);
2199 result.z = (q1.z*0.5f + q2.z*0.5f);
2200 result.w = (q1.w*0.5f + q2.w*0.5f);
2204 float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta;
2205 float ratioB = sinf(amount*halfTheta)/sinHalfTheta;
2207 result.x = (q1.x*ratioA + q2.x*ratioB);
2208 result.y = (q1.y*ratioA + q2.y*ratioB);
2209 result.z = (q1.z*ratioA + q2.z*ratioB);
2210 result.w = (q1.w*ratioA + q2.w*ratioB);
2223 float h00 = 2 * t3 - 3 * t2 + 1;
2224 float h10 = t3 - 2 * t2 + t;
2225 float h01 = -2 * t3 + 3 * t2;
2226 float h11 = t3 - t2;
2229 Quaternion m0 = QuaternionScale(outTangent1, h10);
2231 Quaternion m1 = QuaternionScale(inTangent2, h11);
2235 result = QuaternionAdd(p0, m0);
2236 result = QuaternionAdd(result, p1);
2237 result = QuaternionAdd(result, m1);
2238 result = QuaternionNormalize(result);
2248 float cos2Theta = (from.x*to.x + from.y*to.y + from.z*to.z);
2249 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 };
2254 result.w = 1.0f + cos2Theta;
2259 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2260 if (length == 0.0f) length = 1.0f;
2261 float ilength = 1.0f/length;
2263 result.x = q.x*ilength;
2264 result.y = q.y*ilength;
2265 result.z = q.z*ilength;
2266 result.w = q.w*ilength;
2276 float fourWSquaredMinus1 = mat.m0 + mat.m5 + mat.m10;
2277 float fourXSquaredMinus1 = mat.m0 - mat.m5 - mat.m10;
2278 float fourYSquaredMinus1 = mat.m5 - mat.m0 - mat.m10;
2279 float fourZSquaredMinus1 = mat.m10 - mat.m0 - mat.m5;
2281 int biggestIndex = 0;
2282 float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
2283 if (fourXSquaredMinus1 > fourBiggestSquaredMinus1)
2285 fourBiggestSquaredMinus1 = fourXSquaredMinus1;
2289 if (fourYSquaredMinus1 > fourBiggestSquaredMinus1)
2291 fourBiggestSquaredMinus1 = fourYSquaredMinus1;
2295 if (fourZSquaredMinus1 > fourBiggestSquaredMinus1)
2297 fourBiggestSquaredMinus1 = fourZSquaredMinus1;
2301 float biggestVal = sqrtf(fourBiggestSquaredMinus1 + 1.0f)*0.5f;
2302 float mult = 0.25f/biggestVal;
2304 switch (biggestIndex)
2307 result.w = biggestVal;
2308 result.x = (mat.m6 - mat.m9)*mult;
2309 result.y = (mat.m8 - mat.m2)*mult;
2310 result.z = (mat.m1 - mat.m4)*mult;
2313 result.x = biggestVal;
2314 result.w = (mat.m6 - mat.m9)*mult;
2315 result.y = (mat.m1 + mat.m4)*mult;
2316 result.z = (mat.m8 + mat.m2)*mult;
2319 result.y = biggestVal;
2320 result.w = (mat.m8 - mat.m2)*mult;
2321 result.x = (mat.m1 + mat.m4)*mult;
2322 result.z = (mat.m6 + mat.m9)*mult;
2325 result.z = biggestVal;
2326 result.w = (mat.m1 - mat.m4)*mult;
2327 result.x = (mat.m8 + mat.m2)*mult;
2328 result.y = (mat.m6 + mat.m9)*mult;
2338 Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
2339 0.0f, 1.0f, 0.0f, 0.0f,
2340 0.0f, 0.0f, 1.0f, 0.0f,
2341 0.0f, 0.0f, 0.0f, 1.0f };
2353 result.m0 = 1 - 2*(b2 + c2);
2354 result.m1 = 2*(ab + cd);
2355 result.m2 = 2*(ac - bd);
2357 result.m4 = 2*(ab - cd);
2358 result.m5 = 1 - 2*(a2 + c2);
2359 result.m6 = 2*(bc + ad);
2361 result.m8 = 2*(ac + bd);
2362 result.m9 = 2*(bc - ad);
2363 result.m10 = 1 - 2*(a2 + b2);
2372 Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
2374 float axisLength = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
2376 if (axisLength != 0.0f)
2380 float length = 0.0f;
2381 float ilength = 0.0f;
2384 length = axisLength;
2385 if (length == 0.0f) length = 1.0f;
2386 ilength = 1.0f/length;
2391 float sinres = sinf(angle);
2392 float cosres = cosf(angle);
2394 result.x = axis.x*sinres;
2395 result.y = axis.y*sinres;
2396 result.z = axis.z*sinres;
2401 length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2402 if (length == 0.0f) length = 1.0f;
2403 ilength = 1.0f/length;
2404 result.x = q.x*ilength;
2405 result.y = q.y*ilength;
2406 result.z = q.z*ilength;
2407 result.w = q.w*ilength;
2414 RMAPI
void QuaternionToAxisAngle(
Quaternion q,
Vector3 *outAxis,
float *outAngle)
2416 if (fabsf(q.w) > 1.0f)
2419 float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
2420 if (length == 0.0f) length = 1.0f;
2421 float ilength = 1.0f/length;
2429 Vector3 resAxis = { 0.0f, 0.0f, 0.0f };
2430 float resAngle = 2.0f*acosf(q.w);
2431 float den = sqrtf(1.0f - q.w*q.w);
2435 resAxis.x = q.x/den;
2436 resAxis.y = q.y/den;
2437 resAxis.z = q.z/den;
2447 *outAngle = resAngle;
2452 RMAPI
Quaternion QuaternionFromEuler(
float pitch,
float yaw,
float roll)
2456 float x0 = cosf(pitch*0.5f);
2457 float x1 = sinf(pitch*0.5f);
2458 float y0 = cosf(yaw*0.5f);
2459 float y1 = sinf(yaw*0.5f);
2460 float z0 = cosf(roll*0.5f);
2461 float z1 = sinf(roll*0.5f);
2463 result.x = x1*y0*z0 - x0*y1*z1;
2464 result.y = x0*y1*z0 + x1*y0*z1;
2465 result.z = x0*y0*z1 - x1*y1*z0;
2466 result.w = x0*y0*z0 + x1*y1*z1;
2478 float x0 = 2.0f*(q.w*q.x + q.y*q.z);
2479 float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y);
2480 result.x = atan2f(x0, x1);
2483 float y0 = 2.0f*(q.w*q.y - q.z*q.x);
2484 y0 = y0 > 1.0f ? 1.0f : y0;
2485 y0 = y0 < -1.0f ? -1.0f : y0;
2486 result.y = asinf(y0);
2489 float z0 = 2.0f*(q.w*q.z + q.x*q.y);
2490 float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z);
2491 result.z = atan2f(z0, z1);
2501 result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w;
2502 result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w;
2503 result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w;
2504 result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w;
2512 #if !defined(EPSILON) 2513 #define EPSILON 0.000001f 2516 int result = (((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
2517 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
2518 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
2519 ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))))) ||
2520 (((fabsf(p.x + q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
2521 ((fabsf(p.y + q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
2522 ((fabsf(p.z + q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
2523 ((fabsf(p.w + q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w))))));
Definition: raymath.h:161
Definition: raymath.h:165