diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2022-09-05 08:27:31 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2022-09-05 08:27:31 +0200 |
commit | 53a1d08bfc347021364510291d9e1145b8cb869a (patch) | |
tree | 62ef664fb1aa77b691437b1f3d2753eb20d3dbef /core/math/vector4.cpp | |
parent | 785ce4208db2b64e78ebd90c33d00a7fe04fc01b (diff) | |
parent | 058ac331b07a634f01eea18dbad4161503d407cb (diff) |
Merge pull request #65348 from aaronfranke/fix-vec4
Minor fixes to Vector4 in core
Diffstat (limited to 'core/math/vector4.cpp')
-rw-r--r-- | core/math/vector4.cpp | 19 |
1 files changed, 16 insertions, 3 deletions
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)); |