From d47b4076a9befe284261a3616ab3db7a80142dcf Mon Sep 17 00:00:00 2001 From: THoehne <77296181+THoehne@users.noreply.github.com> Date: Sun, 21 Jul 2024 19:15:52 +0200 Subject: [PATCH] Bug fixes. --- .../Core/public/Math/Detail/Vector3Decl.inl | 4 +- .../Runtime/Core/public/Math/Vector3.hpp | 51 +++++++++---------- .../Runtime/Core/public/Math/Vector3.inl | 1 + .../Runtime/Core/public/Math/Vector4.hpp | 30 +++++++---- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl index 5612c25..d742a39 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl @@ -206,8 +206,10 @@ namespace Phanes::Core::Math::Detail template struct compute_vec3_cross_p { - static constexpr void map(Phanes::Core::Math::TVector3& r, const Phanes::Core::Math::TVector3& v1, const Phanes::Core::Math::TVector3& v2) + static constexpr void map(Phanes::Core::Math::TVector3& r, const Phanes::Core::Math::TVector3 v1, const Phanes::Core::Math::TVector3& v2) { + // V1 has to be copied, as otherwise changes to r affect calculation -> r is v1. + r.x = (v1.y * v2.z) - (v1.z * v2.y); r.y = (v1.z * v2.x) - (v1.x * v2.z); r.z = (v1.x * v2.y) - (v1.y * v2.x); diff --git a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp index 4ca23f4..37d2f5b 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp @@ -533,14 +533,15 @@ namespace Phanes::Core::Math { return v1; } - /** - * Calculates the cross product between two vectors. - * - * @param(v1) Vector one - * @param(v2) Vector two - * - * @note result is stored in v1. - */ + + /// + /// Calcualtes cross product of vectors + /// + /// + /// + /// + /// + /// Copy of v1. template TVector3 CrossPV(TVector3& v1, const TVector3& v2); @@ -620,6 +621,7 @@ namespace Phanes::Core::Math { return v1; } + /** * Projects vector v1 onto v2 * @@ -701,15 +703,13 @@ namespace Phanes::Core::Math { */ template - TVector3 ClampMagnitudeV(TVector3& v1, T min, T max) + TVector3 ClampToMagnitudeV(TVector3& v1, T min, T max) { T magnitude = Magnitude(v1); v1 = (magnitude > P_FLT_INAC) ? v1 / magnitude : PZeroVector3(T, false); - Clamp(magnitude, min, max); - - v1 *= magnitude; + v1 *= Clamp(magnitude, min, max); return v1; } @@ -767,8 +767,7 @@ namespace Phanes::Core::Math { T sinAngle = sin(angle); T cosAngle = cos(angle); - v1 = ((T)1.0 - cosAngle) * DotP(axisNormal, v1) * axisNormal + cosAngle * v1 + sinAngle * CrossP(axisNormal, v1); - + v1 = (1 - cosAngle) * DotP(axisNormal, v1) * axisNormal + cosAngle * v1 + sinAngle * CrossP(v1, axisNormal); return v1; } @@ -783,8 +782,8 @@ namespace Phanes::Core::Math { template TVector3 ScaleToMagnitudeV(TVector3& v1, T magnitude) { - NormalizeV(v1) *= magnitude; - + NormalizeV(v1); + v1 *= magnitude; return v1; } @@ -818,7 +817,7 @@ namespace Phanes::Core::Math { template T ScalarTriple(const TVector3& v1, const TVector3& v2, const TVector3& v3) { - return CrossP(v1, v2) * v3; + return DotP(CrossP(v1, v2), v3); } /** @@ -992,9 +991,9 @@ namespace Phanes::Core::Math { template TVector3 SignVector(const TVector3& v1) { - return TVector3((v1.x >= 0) ? 1 : -1, - (v1.y >= 0) ? 1 : -1, - (v1.z >= 0) ? 1 : -1); + return TVector3((v1.x >= (T)0) ? (T)1 : (T)-1, + (v1.y >= (T)0) ? (T)1 : (T)-1, + (v1.z >= (T)0) ? (T)1 : (T)-1); } /** @@ -1037,8 +1036,8 @@ namespace Phanes::Core::Math { * @return Cross product of v1 and v2 */ - template - TVector3 CrossP(const TVector3& v1, const TVector3& v2); + template + TVector3 CrossP(const TVector3& v1, const TVector3& v2); /** * Linearly interpolates between two vectors. @@ -1053,8 +1052,8 @@ namespace Phanes::Core::Math { template TVector3 Lerp(const TVector3& start, const TVector3& dest, T t) { - t = Clamp(t, (T)0.0, (T), 1.0); - return (1 - t) * start + t * dest; + t = Clamp(t, (T)0.0, (T)1.0); + return ((1 - t) * start) + (t * dest); } /** @@ -1148,7 +1147,7 @@ namespace Phanes::Core::Math { */ template - TVector3 ClampMagnitude(const TVector3& v1, T min, T max) + TVector3 ClampToMagnitude(const TVector3& v1, T min, T max) { T magnitude = Magnitude(v1); @@ -1156,7 +1155,7 @@ namespace Phanes::Core::Math { Clamp(magnitude, min, max); - return unitVec * magnitude; + return unitVec * Clamp(magnitude, min, max); } /** diff --git a/Engine/Source/Runtime/Core/public/Math/Vector3.inl b/Engine/Source/Runtime/Core/public/Math/Vector3.inl index 443970b..bdd0589 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector3.inl +++ b/Engine/Source/Runtime/Core/public/Math/Vector3.inl @@ -226,4 +226,5 @@ namespace Phanes::Core::Math Detail::compute_vec3_cross_p::map(v1, v1, v2); return v1; } + } \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/Vector4.hpp b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp index f733680..5457030 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector4.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp @@ -10,6 +10,8 @@ #include "Core/public/Math/Vector2.hpp" +#define PZeroVector4(type, aligned) Phanes::Core::Math::TVector4<##type, ##aligned>(0,0,0) + namespace Phanes::Core::Math { @@ -680,11 +682,15 @@ namespace Phanes::Core::Math /// /// Vector with magnitude clamped to s. template - TVector4 ClampToMagnitude(const TVector4& v1, T s) + TVector4 ClampToMagnitude(const TVector4& v1, T min, T max) { - float vecNorm = Magnitude(v1); - TVector4 newVec = (vecNorm > s) ? v1 : v1 / Magnitude(v1); - return newVec; + T magnitude = Magnitude(v1); + + const TVector3 unitVec = (magnitude > P_FLT_INAC) ? v1 / magnitude : PZeroVector3(T, false); + + Clamp(magnitude, min, max); + + return unitVec * magnitude; } /// @@ -696,10 +702,14 @@ namespace Phanes::Core::Math /// Magnitude /// Copy of v1. template - TVector4 ClampToMagnitudeV(TVector4& v1, T s) + TVector4 ClampToMagnitudeV(TVector4& v1, T min, T max) { - float vecNorm = Magnitude(v1); - v1 = (vecNorm > s) ? v1 : v1 / Magnitude(v1); + T magnitude = Magnitude(v1); + + v1 = (magnitude > P_FLT_INAC) ? v1 / magnitude : PZeroVector3(T, false); + + v1 *= Clamp(magnitude, min, max); + return v1; } @@ -714,8 +724,7 @@ namespace Phanes::Core::Math template TVector4 ScaleToMagnitude(const TVector4& v1, T s) { - TVector4 vecDir = v1 / Magnitude(v1); - return vecDir * s; + return Normalize(v1) * s; } /// @@ -729,8 +738,7 @@ namespace Phanes::Core::Math template TVector4 ScaleToMagnitudeV(TVector4& v1, T s) { - v1 /= Magnitude(v1); - v1 *= s; + NormalizeV(v1) *= s; return v1; }