diff --git a/core/math/vector2i.h b/core/math/vector2i.h
index e6850347c30..b2c75beb4dd 100644
--- a/core/math/vector2i.h
+++ b/core/math/vector2i.h
@@ -85,6 +85,14 @@ struct _NO_DISCARD_ Vector2i {
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
}
+ double distance_to(const Vector2i &p_to) const {
+ return (p_to - *this).length();
+ }
+
+ int64_t distance_squared_to(const Vector2i &p_to) const {
+ return (p_to - *this).length_squared();
+ }
+
Vector2i operator+(const Vector2i &p_v) const;
void operator+=(const Vector2i &p_v);
Vector2i operator-(const Vector2i &p_v) const;
diff --git a/core/math/vector3i.h b/core/math/vector3i.h
index 53d3829a99d..5a5e9deda85 100644
--- a/core/math/vector3i.h
+++ b/core/math/vector3i.h
@@ -87,6 +87,9 @@ struct _NO_DISCARD_ Vector3i {
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
Vector3i snapped(const Vector3i &p_step) const;
+ _FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
+ _FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
+
/* Operators */
_FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
@@ -143,6 +146,14 @@ Vector3i Vector3i::sign() const {
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
}
+double Vector3i::distance_to(const Vector3i &p_to) const {
+ return (p_to - *this).length();
+}
+
+int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
+ return (p_to - *this).length_squared();
+}
+
/* Operators */
Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
diff --git a/core/math/vector4i.h b/core/math/vector4i.h
index b815aa8e768..7d85d473d93 100644
--- a/core/math/vector4i.h
+++ b/core/math/vector4i.h
@@ -84,6 +84,9 @@ struct _NO_DISCARD_ Vector4i {
_FORCE_INLINE_ void zero();
+ _FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
+ _FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
+
_FORCE_INLINE_ Vector4i abs() const;
_FORCE_INLINE_ Vector4i sign() const;
Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
@@ -139,6 +142,14 @@ double Vector4i::length() const {
return Math::sqrt((double)length_squared());
}
+double Vector4i::distance_to(const Vector4i &p_to) const {
+ return (p_to - *this).length();
+}
+
+int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
+ return (p_to - *this).length_squared();
+}
+
Vector4i Vector4i::abs() const {
return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
}
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index f041d2c95ee..34679cd4d67 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1796,6 +1796,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2i, aspect, sarray(), varray());
bind_method(Vector2i, max_axis_index, sarray(), varray());
bind_method(Vector2i, min_axis_index, sarray(), varray());
+ bind_method(Vector2i, distance_to, sarray("to"), varray());
+ bind_method(Vector2i, distance_squared_to, sarray("to"), varray());
bind_method(Vector2i, length, sarray(), varray());
bind_method(Vector2i, length_squared, sarray(), varray());
bind_method(Vector2i, sign, sarray(), varray());
@@ -1886,6 +1888,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector3i, min_axis_index, sarray(), varray());
bind_method(Vector3i, max_axis_index, sarray(), varray());
+ bind_method(Vector3i, distance_to, sarray("to"), varray());
+ bind_method(Vector3i, distance_squared_to, sarray("to"), varray());
bind_method(Vector3i, length, sarray(), varray());
bind_method(Vector3i, length_squared, sarray(), varray());
bind_method(Vector3i, sign, sarray(), varray());
@@ -1932,6 +1936,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector4i, abs, sarray(), varray());
bind_method(Vector4i, clamp, sarray("min", "max"), varray());
bind_method(Vector4i, snapped, sarray("step"), varray());
+ bind_method(Vector4i, distance_to, sarray("to"), varray());
+ bind_method(Vector4i, distance_squared_to, sarray("to"), varray());
/* Plane */
diff --git a/doc/classes/Vector2i.xml b/doc/classes/Vector2i.xml
index 2100cd76121..e0047558189 100644
--- a/doc/classes/Vector2i.xml
+++ b/doc/classes/Vector2i.xml
@@ -64,6 +64,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
+
+
+
+
+ Returns the squared distance between this vector and [param to].
+ This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
+
+
+
+
+
+
+ Returns the distance between this vector and [param to].
+
+
diff --git a/doc/classes/Vector3i.xml b/doc/classes/Vector3i.xml
index 8906bf0aa71..96760ed9f9f 100644
--- a/doc/classes/Vector3i.xml
+++ b/doc/classes/Vector3i.xml
@@ -59,6 +59,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
+
+
+
+
+ Returns the squared distance between this vector and [param to].
+ This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
+
+
+
+
+
+
+ Returns the distance between this vector and [param to].
+
+
diff --git a/doc/classes/Vector4i.xml b/doc/classes/Vector4i.xml
index a612c135ddc..464c19c2230 100644
--- a/doc/classes/Vector4i.xml
+++ b/doc/classes/Vector4i.xml
@@ -57,6 +57,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
+
+
+
+
+ Returns the squared distance between this vector and [param to].
+ This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
+
+
+
+
+
+
+ Returns the distance between this vector and [param to].
+
+
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2I.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2I.cs
index 4ee452455e0..07697e5c991 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2I.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2I.cs
@@ -120,6 +120,29 @@ namespace Godot
);
}
+ ///
+ /// Returns the squared distance between this vector and .
+ /// This method runs faster than , so prefer it if
+ /// you need to compare vectors or need the squared distance for some formula.
+ ///
+ /// The other vector to use.
+ /// The squared distance between the two vectors.
+ public readonly int DistanceSquaredTo(Vector2I to)
+ {
+ return (to - this).LengthSquared();
+ }
+
+ ///
+ /// Returns the distance between this vector and .
+ ///
+ ///
+ /// The other vector to use.
+ /// The distance between the two vectors.
+ public readonly real_t DistanceTo(Vector2I to)
+ {
+ return (to - this).Length();
+ }
+
///
/// Returns the length (magnitude) of this vector.
///
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3I.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3I.cs
index db8ceb30e9d..2651d9c8f88 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3I.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3I.cs
@@ -128,6 +128,29 @@ namespace Godot
);
}
+ ///
+ /// Returns the squared distance between this vector and .
+ /// This method runs faster than , so prefer it if
+ /// you need to compare vectors or need the squared distance for some formula.
+ ///
+ /// The other vector to use.
+ /// The squared distance between the two vectors.
+ public readonly int DistanceSquaredTo(Vector3I to)
+ {
+ return (to - this).LengthSquared();
+ }
+
+ ///
+ /// Returns the distance between this vector and .
+ ///
+ ///
+ /// The other vector to use.
+ /// The distance between the two vectors.
+ public readonly real_t DistanceTo(Vector3I to)
+ {
+ return (to - this).Length();
+ }
+
///
/// Returns the length (magnitude) of this vector.
///
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4I.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4I.cs
index e75e996b04d..3da2aaa90dc 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4I.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4I.cs
@@ -145,6 +145,29 @@ namespace Godot
);
}
+ ///
+ /// Returns the squared distance between this vector and .
+ /// This method runs faster than , so prefer it if
+ /// you need to compare vectors or need the squared distance for some formula.
+ ///
+ /// The other vector to use.
+ /// The squared distance between the two vectors.
+ public readonly int DistanceSquaredTo(Vector4I to)
+ {
+ return (to - this).LengthSquared();
+ }
+
+ ///
+ /// Returns the distance between this vector and .
+ ///
+ ///
+ /// The other vector to use.
+ /// The distance between the two vectors.
+ public readonly real_t DistanceTo(Vector4I to)
+ {
+ return (to - this).Length();
+ }
+
///
/// Returns the length (magnitude) of this vector.
///
diff --git a/tests/core/math/test_vector2i.h b/tests/core/math/test_vector2i.h
index 743c87f486a..0f33400f7f5 100644
--- a/tests/core/math/test_vector2i.h
+++ b/tests/core/math/test_vector2i.h
@@ -87,6 +87,12 @@ TEST_CASE("[Vector2i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(36.05551275463989293119),
"Vector2i length should work as expected.");
+ CHECK_MESSAGE(
+ vector1.distance_squared_to(vector2) == 500,
+ "Vector2i distance_squared_to should work as expected and return exact result.");
+ CHECK_MESSAGE(
+ vector1.distance_to(vector2) == doctest::Approx(22.36067977499789696409),
+ "Vector2i distance_to should work as expected.");
}
TEST_CASE("[Vector2i] Operators") {
diff --git a/tests/core/math/test_vector3i.h b/tests/core/math/test_vector3i.h
index 485a5007152..3914b85a750 100644
--- a/tests/core/math/test_vector3i.h
+++ b/tests/core/math/test_vector3i.h
@@ -90,6 +90,12 @@ TEST_CASE("[Vector3i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(53.8516480713450403125),
"Vector3i length should work as expected.");
+ CHECK_MESSAGE(
+ vector1.distance_squared_to(vector2) == 1400,
+ "Vector3i distance_squared_to should work as expected and return exact result.");
+ CHECK_MESSAGE(
+ vector1.distance_to(vector2) == doctest::Approx(37.41657386773941385584),
+ "Vector3i distance_to should work as expected.");
}
TEST_CASE("[Vector3i] Operators") {
diff --git a/tests/core/math/test_vector4i.h b/tests/core/math/test_vector4i.h
index 5fda6f17783..31f68696c05 100644
--- a/tests/core/math/test_vector4i.h
+++ b/tests/core/math/test_vector4i.h
@@ -90,6 +90,12 @@ TEST_CASE("[Vector4i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(73.4846922835),
"Vector4i length should work as expected.");
+ CHECK_MESSAGE(
+ vector1.distance_squared_to(vector2) == 3000,
+ "Vector4i distance_squared_to should work as expected.");
+ CHECK_MESSAGE(
+ vector1.distance_to(vector2) == doctest::Approx(54.772255750517),
+ "Vector4i distance_to should work as expected.");
}
TEST_CASE("[Vector4i] Operators") {