From 40a1d6d100d8a1823d3a939a7ad7350b36bcddbd Mon Sep 17 00:00:00 2001 From: antonWetzel Date: Mon, 8 Aug 2022 17:05:55 +0200 Subject: vector4 distance_squared_to and update csharp --- core/math/vector4.cpp | 4 + core/math/vector4.h | 1 + core/variant/variant_call.cpp | 1 + doc/classes/Vector4.xml | 8 ++ .../glue/GodotSharp/GodotSharp/Core/Vector4.cs | 112 ++++++++++++++++++++- tests/core/math/test_vector4.h | 3 + 6 files changed, 124 insertions(+), 5 deletions(-) diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index 4697c311b4..1dd5adad2b 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -91,6 +91,10 @@ real_t Vector4::distance_to(const Vector4 &p_to) const { return (p_to - *this).length(); } +real_t Vector4::distance_squared_to(const Vector4 &p_to) const { + return (p_to - *this).length_squared(); +} + Vector4 Vector4::direction_to(const Vector4 &p_to) const { Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w); ret.normalize(); diff --git a/core/math/vector4.h b/core/math/vector4.h index 373a6a1218..d26fe15941 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -79,6 +79,7 @@ struct _NO_DISCARD_ Vector4 { bool is_normalized() const; real_t distance_to(const Vector4 &p_to) const; + real_t distance_squared_to(const Vector4 &p_to) const; Vector4 direction_to(const Vector4 &p_to) const; Vector4 abs() const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 5c298f9b3b..f3f2eb16c4 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1746,6 +1746,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, is_normalized, sarray(), varray()); bind_method(Vector4, direction_to, sarray("to"), varray()); bind_method(Vector4, distance_to, sarray("to"), varray()); + bind_method(Vector4, distance_squared_to, sarray("to"), varray()); bind_method(Vector4, dot, sarray("with"), varray()); bind_method(Vector4, inverse, sarray(), varray()); bind_method(Vector4, is_equal_approx, sarray("with"), varray()); diff --git a/doc/classes/Vector4.xml b/doc/classes/Vector4.xml index b5658f074a..a93689c619 100644 --- a/doc/classes/Vector4.xml +++ b/doc/classes/Vector4.xml @@ -80,6 +80,14 @@ Returns the normalized vector pointing from this vector to [code]to[/code]. This is equivalent to using [code](b - a).normalized()[/code]. + + + + + Returns the squared distance between this vector and [code]to[/code]. + 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. + + diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4.cs index 72fe9cb16f..4af817455c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4.cs @@ -140,7 +140,6 @@ namespace Godot } } - /// /// Returns a new vector with all components in absolute values (i.e. positive). /// @@ -178,16 +177,59 @@ namespace Godot ); } + /// + /// Performs a cubic interpolation between vectors , this vector, + /// , and , by the given amount . + /// + /// The destination vector. + /// A vector before this vector. + /// A vector after . + /// A value on the range of 0.0 to 1.0, representing the amount of interpolation. + /// The interpolated vector. + public Vector4 CubicInterpolate(Vector4 b, Vector4 preA, Vector4 postB, real_t weight) + { + return new Vector4 + ( + Mathf.CubicInterpolate(x, b.x, preA.x, postB.x, weight), + Mathf.CubicInterpolate(y, b.y, preA.y, postB.y, weight), + Mathf.CubicInterpolate(y, b.z, preA.z, postB.z, weight), + Mathf.CubicInterpolate(w, b.w, preA.w, postB.w, weight) + ); + } /// - /// Returns a new vector with all components rounded down (towards negative infinity). + /// Returns the normalized vector pointing from this vector to . /// - /// A vector with called on each component. - public Vector4 Floor() + /// The other vector to point towards. + /// The direction from this vector to . + public Vector4 DirectionTo(Vector4 to) { - return new Vector4(Mathf.Floor(x), Mathf.Floor(y), Mathf.Floor(z), Mathf.Floor(w)); + Vector4 ret = new Vector4(to.x - x, to.y - y, to.z - z, to.w - w); + ret.Normalize(); + return ret; } + /// + /// 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 real_t DistanceSquaredTo(Vector4 to) + { + return (to - this).LengthSquared(); + } + + /// + /// Returns the distance between this vector and . + /// + /// The other vector to use. + /// The distance between the two vectors. + public real_t DistanceTo(Vector4 to) + { + return (to - this).Length(); + } /// /// Returns the dot product of this vector and . @@ -199,6 +241,15 @@ namespace Godot return (x * with.x) + (y * with.y) + (z * with.z) + (w + with.w); } + /// + /// Returns a new vector with all components rounded down (towards negative infinity). + /// + /// A vector with called on each component. + public Vector4 Floor() + { + return new Vector4(Mathf.Floor(x), Mathf.Floor(y), Mathf.Floor(z), Mathf.Floor(w)); + } + /// /// Returns the inverse of this vector. This is the same as new Vector4(1 / v.x, 1 / v.y, 1 / v.z, 1 / v.w). /// @@ -317,6 +368,42 @@ namespace Godot return v; } + /// + /// Returns a vector composed of the of this vector's components + /// and . + /// + /// A value representing the divisor of the operation. + /// + /// A vector with each component by . + /// + public Vector4 PosMod(real_t mod) + { + return new Vector4( + Mathf.PosMod(x, mod), + Mathf.PosMod(y, mod), + Mathf.PosMod(z, mod), + Mathf.PosMod(w, mod) + ); + } + + /// + /// Returns a vector composed of the of this vector's components + /// and 's components. + /// + /// A vector representing the divisors of the operation. + /// + /// A vector with each component by 's components. + /// + public Vector4 PosMod(Vector4 modv) + { + return new Vector4( + Mathf.PosMod(x, modv.x), + Mathf.PosMod(y, modv.y), + Mathf.PosMod(z, modv.z), + Mathf.PosMod(w, modv.w) + ); + } + /// /// Returns this vector with all components rounded to the nearest integer, /// with halfway cases rounded towards the nearest multiple of two. @@ -343,6 +430,21 @@ namespace Godot return v; } + /// + /// Returns this vector with each component snapped to the nearest multiple of . + /// This can also be used to round to an arbitrary number of decimals. + /// + /// A vector value representing the step size to snap to. + public Vector4 Snapped(Vector4 step) + { + return new Vector4( + Mathf.Snapped(x, step.x), + Mathf.Snapped(y, step.y), + Mathf.Snapped(z, step.z), + Mathf.Snapped(w, step.w) + ); + } + // Constants private static readonly Vector4 _zero = new Vector4(0, 0, 0, 0); private static readonly Vector4 _one = new Vector4(1, 1, 1, 1); diff --git a/tests/core/math/test_vector4.h b/tests/core/math/test_vector4.h index 4b8759c0ca..ccf991401b 100644 --- a/tests/core/math/test_vector4.h +++ b/tests/core/math/test_vector4.h @@ -98,6 +98,9 @@ TEST_CASE("[Vector4] Length methods") { CHECK_MESSAGE( Math::is_equal_approx(vector1.distance_to(vector2), (real_t)54.772255750517), "Vector4 distance_to should work as expected."); + CHECK_MESSAGE( + Math::is_equal_approx(vector1.distance_squared_to(vector2), 3000), + "Vector4 distance_squared_to should work as expected."); } TEST_CASE("[Vector4] Limiting methods") { -- cgit v1.2.3