summaryrefslogtreecommitdiff
path: root/core/math/vector4.cpp
diff options
context:
space:
mode:
authorAaron Franke <arnfranke@yahoo.com>2022-09-04 20:16:59 -0500
committerAaron Franke <arnfranke@yahoo.com>2022-09-04 20:38:44 -0500
commit058ac331b07a634f01eea18dbad4161503d407cb (patch)
treeda26b8748c26023385e87b9fbde20ccf823e180a /core/math/vector4.cpp
parente7a0a97c0bc5d86644e62d537503e3b05313a01c (diff)
Minor fixes to Vector4
Diffstat (limited to 'core/math/vector4.cpp')
-rw-r--r--core/math/vector4.cpp19
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));