From e10f9d8e7517c20a25f8f0abf192aec088ada3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 12 Mar 2025 21:31:47 +0100 Subject: [PATCH] fix: vector3 and vector4 functions --- src/math/vector3.c | 39 ++++++++++++++++++++++++++++----------- src/math/vector4.c | 15 +++++++++++---- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/math/vector3.c b/src/math/vector3.c index b570c5f..ab23ce2 100644 --- a/src/math/vector3.c +++ b/src/math/vector3.c @@ -53,7 +53,7 @@ Vec3* vec3Norm(Vec3* v) if (length == 0.f) return vec3(0, 0, 0); float invLength = 1.f / length; - return vec3(v->x * invLength, v->y * invLength, v->z * invLength); + return vec3Scale(v, invLength); } Vec3* vec3Lerp(Vec3* a, Vec3* b, float t) @@ -90,18 +90,26 @@ Vec3* vec3Proj(Vec3* a, Vec3* b) float dotAB = vec3Dot(a, b); float lenB2 = vec3Dot(b, b); float scale = dotAB / lenB2; - return vec3(b->x * scale, b->y * scale, b->z * scale); + return vec3Scale(b, scale); } Vec3* vec3Refl(Vec3* v, Vec3* normal) { + if (!v || !normal) return NULL; Vec3* proj = vec3Proj(v, normal); - return vec3Sub(v, vec3Scale(proj, 2.f)); + Vec3* scal = vec3Scale(proj, 2.f); + Vec3* rlt = vec3Sub(v, scal); + vec3Free(proj); + vec3Free(scal); + return rlt; } float vec3Dist(Vec3* a, Vec3* b) { - return vec3Len(vec3Sub(a, b)); + Vec3* vsub = vec3Sub(a, b); + float rlt = vec3Len(vsub); + vec3Free(vsub); + return rlt; } Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle) @@ -110,13 +118,22 @@ Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle) float dot = vec3Dot(axis, v); Vec3* cross = vec3Cross(axis, v); - return vec3Add( - vec3Add( - vec3Scale(v, cosf(angle)), - vec3Scale(cross, sinf(angle)) - ), - vec3Scale(axis, dot * (1 - cosf(angle))) - ); + Vec3* vscal = vec3Scale(v, cosf(angle)); + Vec3* cscal = vec3Scale(cross, sinf(angle)); + + Vec3* add = vec3Add(vscal, cscal); + Vec3* dscal = vec3Scale(axis, dot * (1 - cosf(angle))); + + Vec3* rlt = vec3Add(add, dscal); + + vec3Free(cross); + vec3Free(vscal); + vec3Free(cscal); + vec3Free(add); + vec3Free(dscal); + + return rlt; + } void vec3Free(Vec3* v) diff --git a/src/math/vector4.c b/src/math/vector4.c index 32742d8..e8c9c65 100644 --- a/src/math/vector4.c +++ b/src/math/vector4.c @@ -54,7 +54,7 @@ Vec4* vec4Norm(Vec4* v) if (length == 0.f) return vec4(0, 0, 0, 0); float invLength = 1.f / length; - return vec4(v->x * invLength, v->y * invLength, v->z * invLength, v->w * invLength); + return vec4Scale(v, invLength); } Vec4* vec4Lerp(Vec4* a, Vec4* b, float t) @@ -85,19 +85,26 @@ Vec4* vec4Proj(Vec4* a, Vec4* b) float dotAB = vec4Dot(a, b); float lenB2 = vec4Dot(b, b); float scale = dotAB / lenB2; - return vec4(b->x * scale, b->y * scale, b->z * scale, 0); + return vec4Scale(b, scale); } Vec4* vec4Refl(Vec4* v, Vec4* normal) { if (!v || !normal) return NULL; Vec4* proj = vec4Proj(v, normal); - return vec4Sub(v, vec4Scale(proj, 2.f)); + Vec4* scal = vec4Scale(proj, 2.f); + Vec4* rlt = vec4Sub(v, scal); + vec4Free(proj); + vec4Free(scal); + return rlt; } float vec4Dist(Vec4* a, Vec4* b) { - return vec4Len(vec4Sub(a, b)); + Vec4* vsub = vec4Sub(a, b); + float rlt = vec4Len(vsub); + vec4Free(vsub); + return rlt; } void vec4Free(Vec4* v)