From 5fb6a47ce5abeee37a9456564f486fa478499283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sun, 16 Mar 2025 17:54:53 +0100 Subject: [PATCH] fix(vector3, vector4): fix some functions. --- src/math/vector3.c | 2 +- src/math/vector4.c | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/math/vector3.c b/src/math/vector3.c index 4b4ebb5..16e5508 100644 --- a/src/math/vector3.c +++ b/src/math/vector3.c @@ -97,7 +97,7 @@ float vec3Angle(Vec3* a, Vec3* b) Vec3* vec3Proj(Vec3* a, Vec3* b) { - + if (!a || !b) return NULL; float dotA = vec3Dot(a, b); float dotB = vec3Dot(b, b); diff --git a/src/math/vector4.c b/src/math/vector4.c index e8c9c65..e3f9bdf 100644 --- a/src/math/vector4.c +++ b/src/math/vector4.c @@ -1,5 +1,6 @@ #include #include +#include #include "math/vector4.h" @@ -42,8 +43,8 @@ float vec4Dot(Vec4* a, Vec4* b) float vec4Len(Vec4* v) { - if (!v) return 0; - return sqrtf(vec4Dot(v, v)); + if (!v) return NAN; + return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); } Vec4* vec4Norm(Vec4* v) @@ -51,17 +52,15 @@ Vec4* vec4Norm(Vec4* v) if (!v) return NULL; float length = vec4Len(v); - if (length == 0.f) return vec4(0, 0, 0, 0); + if (length == 0.f) return NULL; - float invLength = 1.f / length; - return vec4Scale(v, invLength); + return vec4Scale(v, 1.f / length); } Vec4* vec4Lerp(Vec4* a, Vec4* b, float t) { if (!a || !b) return NULL; - if (t < 0.f) t = 0.f; - if (t > 1.f) t = 1.f; + t = fmaxf(0.f, fminf(t, 1.f)); return vec4( a->x + t * (b->x - a->x), @@ -73,27 +72,42 @@ Vec4* vec4Lerp(Vec4* a, Vec4* b, float t) float vec4Angle(Vec4* a, Vec4* b) { - float dot = vec4Dot(a, b); float lenA = vec4Len(a); float lenB = vec4Len(b); - return acosf(dot / (lenA * lenB)); + + if (isnan(lenA) || isnan(lenB) + || lenA < FLT_EPSILON + || lenB < FLT_EPSILON) + return NAN; + + float dot = vec4Dot(a, b); + float cosTheta = dot / (lenA * lenB); + + cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f)); + + return acosf(cosTheta); } Vec4* vec4Proj(Vec4* a, Vec4* b) { if (!a || !b) return NULL; - float dotAB = vec4Dot(a, b); - float lenB2 = vec4Dot(b, b); - float scale = dotAB / lenB2; - return vec4Scale(b, scale); + float dotA = vec4Dot(a, b); + float dotB = vec4Dot(b, b); + + if (dotB < FLT_EPSILON) return NULL; + + float scale = dotA / dotB; + return vec4Scale(b, scale); } Vec4* vec4Refl(Vec4* v, Vec4* normal) { if (!v || !normal) return NULL; + Vec4* proj = vec4Proj(v, normal); Vec4* scal = vec4Scale(proj, 2.f); Vec4* rlt = vec4Sub(v, scal); + vec4Free(proj); vec4Free(scal); return rlt; @@ -101,7 +115,9 @@ Vec4* vec4Refl(Vec4* v, Vec4* normal) float vec4Dist(Vec4* a, Vec4* b) { + if (!a || !b) return NAN; Vec4* vsub = vec4Sub(a, b); + float rlt = vec4Len(vsub); vec4Free(vsub); return rlt;