From 058ac331b07a634f01eea18dbad4161503d407cb Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Sun, 4 Sep 2022 20:16:59 -0500 Subject: Minor fixes to Vector4 --- core/math/vector4.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'core/math/vector4.cpp') diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index fb651fafce..3c25f454a3 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -80,15 +80,26 @@ real_t Vector4::length() const { } void Vector4::normalize() { - *this /= length(); + real_t lengthsq = length_squared(); + if (lengthsq == 0) { + x = y = z = w = 0; + } else { + real_t length = Math::sqrt(lengthsq); + x /= length; + y /= length; + z /= length; + w /= length; + } } Vector4 Vector4::normalized() const { - return *this / length(); + Vector4 v = *this; + v.normalize(); + return v; } bool Vector4::is_normalized() const { - return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon. + return Math::is_equal_approx(length_squared(), (real_t)1, (real_t)UNIT_EPSILON); } real_t Vector4::distance_to(const Vector4 &p_to) const { @@ -187,3 +198,5 @@ Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const { Vector4::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")"; } + +static_assert(sizeof(Vector4) == 4 * sizeof(real_t)); -- cgit v1.2.3