From 7a73c04688e2169ad04d77530e50218e6355254d Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Sat, 16 Sep 2023 21:18:00 +0200 Subject: [PATCH 1/9] SmoothDamp implementation for Vector2 and Vector3 --- Source/Engine/Core/Math/Vector2.cs | 86 ++++++++++++++++++++++++++ Source/Engine/Core/Math/Vector3.cs | 98 ++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 2af365638..4b6fb5e59 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -58,6 +58,7 @@ using Mathr = FlaxEngine.Mathf; */ using System; using System.Globalization; +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -953,6 +954,91 @@ namespace FlaxEngine return result; } + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + /// Defines the upper limit on the speed of the Smooth Damp. + public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed) + { + return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.DeltaTime); + } + + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime) + { + return SmoothDamp(current, target, ref currentVelocity, smoothTime, float.PositiveInfinity, Time.DeltaTime); + } + + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + /// Defines the upper limit on the speed of the Smooth Damp. + /// Delta Time, represents the time elapsed since last frame. + public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime) + { + smoothTime = Mathf.Max(0.0001f, smoothTime); + float a = 2f / smoothTime; + float b = a * deltaTime; + float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); + + float change_x = current.X - target.X; + float change_y = current.Y - target.Y; + Vector2 originalTo = target; + + float change = maxSpeed * smoothTime; + float changeSq = change * change; + float sqrmag = change_x * change_x + change_y * change_y; + if (sqrmag > changeSq) + { + var mag = (float)Math.Sqrt(sqrmag); + change_x = change_x / mag * change; + change_y = change_y / mag * change; + } + + target.X = current.X - change_x; + target.Y = current.Y - change_y; + + float temp_x = (currentVelocity.X + a * change_x) * deltaTime; + float temp_y = (currentVelocity.Y + a * change_y) * deltaTime; + + currentVelocity.X = (currentVelocity.X - a * temp_x) * e; + currentVelocity.Y = (currentVelocity.Y - a * temp_y) * e; + + float output_x = target.X + (change_x + temp_x) * e; + float output_y = target.Y + (change_y + temp_y) * e; + + float x1 = originalTo.X - current.X; + float y1 = originalTo.Y - current.Y; + + float x2 = output_x - originalTo.X; + float y2 = output_y - originalTo.Y; + + if (x1 * x2 + y1 * y2 > 0) + { + output_x = originalTo.X; + output_y = originalTo.Y; + + currentVelocity.X = (output_x - originalTo.X) / deltaTime; + currentVelocity.Y = (output_y - originalTo.Y) / deltaTime; + } + + return new Vector2(output_x, output_y); + } + /// /// Performs a cubic interpolation between two vectors. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 4442d3334..c1ea6bb67 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -58,6 +58,7 @@ using Mathr = FlaxEngine.Mathf; */ using System; using System.Globalization; +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -1042,6 +1043,103 @@ namespace FlaxEngine return result; } + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + /// Defines the upper limit on the speed of the Smooth Damp. + public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed) + { + return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.DeltaTime); + } + + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime) + { + return SmoothDamp(current, target, ref currentVelocity, smoothTime, float.PositiveInfinity, Time.DeltaTime); + } + + /// + /// Performs a gradual change of a vector towards a specified target over time + /// + /// Current vector. + /// Target vector. + /// Used to store the current velocity. + /// Determines the approximate time it should take to reach the target vector. + /// Defines the upper limit on the speed of the Smooth Damp. + /// Delta Time, represents the time elapsed since last frame. + public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime) + { + smoothTime = Mathf.Max(0.0001f, smoothTime); + float a = 2f / smoothTime; + float b = a * deltaTime; + float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); + + float change_x = current.X - target.X; + float change_y = current.Y - target.Y; + float change_z = current.Z - target.Z; + + Vector3 originalTo = target; + + float maxChangeSpeed = maxSpeed * smoothTime; + float changeSq = maxChangeSpeed * maxChangeSpeed; + float sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; + if (sqrMag > changeSq) + { + var mag = (float)Math.Sqrt(sqrMag); + change_x = change_x / mag * maxChangeSpeed; + change_y = change_y / mag * maxChangeSpeed; + change_z = change_z / mag * maxChangeSpeed; + } + + target.X = current.X - change_x; + target.Y = current.Y - change_y; + target.Z = current.Z - change_z; + + float temp_x = (currentVelocity.X + a * change_x) * deltaTime; + float temp_y = (currentVelocity.Y + a * change_y) * deltaTime; + float temp_z = (currentVelocity.Z + a * change_z) * deltaTime; + + currentVelocity.X = (currentVelocity.X - a * temp_x) * e; + currentVelocity.Y = (currentVelocity.Y - a * temp_y) * e; + currentVelocity.Z = (currentVelocity.Z - a * temp_z) * e; + + float output_x = target.X + (change_x + temp_x) * e; + float output_y = target.Y + (change_y + temp_y) * e; + float output_z = target.Z + (change_z + temp_z) * e; + + float x1 = originalTo.X - current.X; + float y1 = originalTo.Y - current.Y; + float z1 = originalTo.Z - current.Z; + + float x2 = output_x - originalTo.X; + float y2 = output_y - originalTo.Y; + float z2 = output_z - originalTo.Z; + + if (x1 * x2 + y1 * y2 + z1 * z2 > 0) + { + output_x = originalTo.X; + output_y = originalTo.Y; + output_z = originalTo.Z; + + currentVelocity.X = (output_x - originalTo.X) / deltaTime; + currentVelocity.Y = (output_y - originalTo.Y) / deltaTime; + currentVelocity.Z = (output_z - originalTo.Z) / deltaTime; + } + + return new Vector3(output_x, output_y, output_z); + } + + /// /// Performs a cubic interpolation between two vectors. /// From d87a60de4848c1e9c7312d518b6c1c92e925095e Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Sat, 16 Sep 2023 22:27:12 +0200 Subject: [PATCH 2/9] Naming scheme for Vector2 fix --- Source/Engine/Core/Math/Vector2.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 4b6fb5e59..9fe3c6944 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -1001,12 +1001,12 @@ namespace FlaxEngine float change = maxSpeed * smoothTime; float changeSq = change * change; - float sqrmag = change_x * change_x + change_y * change_y; - if (sqrmag > changeSq) + float sqrDist = change_x * change_x + change_y * change_y; + if (sqrDist > changeSq) { - var mag = (float)Math.Sqrt(sqrmag); - change_x = change_x / mag * change; - change_y = change_y / mag * change; + var dist = (float)Math.Sqrt(sqrDist); + change_x = change_x / dist * change; + change_y = change_y / dist * change; } target.X = current.X - change_x; From 831500faa7d6dd08956774492a7d8be9f67a2d6e Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Sun, 17 Sep 2023 23:16:08 +0200 Subject: [PATCH 3/9] Fix type definition for Vector2 and Vector3 --- Source/Engine/Core/Math/Vector2.cs | 30 ++++++++++++++-------------- Source/Engine/Core/Math/Vector3.cs | 32 +++++++++++++++--------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 9fe3c6944..cb0cd17d1 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -995,37 +995,37 @@ namespace FlaxEngine float b = a * deltaTime; float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); - float change_x = current.X - target.X; - float change_y = current.Y - target.Y; + Real change_x = current.X - target.X; + Real change_y = current.Y - target.Y; Vector2 originalTo = target; - float change = maxSpeed * smoothTime; - float changeSq = change * change; + float maxChangeSpeed = maxSpeed * smoothTime; + float changeSq = maxChangeSpeed * maxChangeSpeed; float sqrDist = change_x * change_x + change_y * change_y; if (sqrDist > changeSq) { - var dist = (float)Math.Sqrt(sqrDist); - change_x = change_x / dist * change; - change_y = change_y / dist * change; + var dist = (Real)Math.Sqrt(sqrDist); + change_x = change_x / dist * maxChangeSpeed; + change_y = change_y / dist * maxChangeSpeed; } target.X = current.X - change_x; target.Y = current.Y - change_y; - float temp_x = (currentVelocity.X + a * change_x) * deltaTime; - float temp_y = (currentVelocity.Y + a * change_y) * deltaTime; + Real temp_x = (currentVelocity.X + a * change_x) * deltaTime; + Real temp_y = (currentVelocity.Y + a * change_y) * deltaTime; currentVelocity.X = (currentVelocity.X - a * temp_x) * e; currentVelocity.Y = (currentVelocity.Y - a * temp_y) * e; - float output_x = target.X + (change_x + temp_x) * e; - float output_y = target.Y + (change_y + temp_y) * e; + Real output_x = target.X + (change_x + temp_x) * e; + Real output_y = target.Y + (change_y + temp_y) * e; - float x1 = originalTo.X - current.X; - float y1 = originalTo.Y - current.Y; + Real x1 = originalTo.X - current.X; + Real y1 = originalTo.Y - current.Y; - float x2 = output_x - originalTo.X; - float y2 = output_y - originalTo.Y; + Real x2 = output_x - originalTo.X; + Real y2 = output_y - originalTo.Y; if (x1 * x2 + y1 * y2 > 0) { diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index c1ea6bb67..53ec824fe 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1084,9 +1084,9 @@ namespace FlaxEngine float b = a * deltaTime; float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); - float change_x = current.X - target.X; - float change_y = current.Y - target.Y; - float change_z = current.Z - target.Z; + Real change_x = current.X - target.X; + Real change_y = current.Y - target.Y; + Real change_z = current.Z - target.Z; Vector3 originalTo = target; @@ -1095,7 +1095,7 @@ namespace FlaxEngine float sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; if (sqrMag > changeSq) { - var mag = (float)Math.Sqrt(sqrMag); + var mag = (Real)Math.Sqrt(sqrMag); change_x = change_x / mag * maxChangeSpeed; change_y = change_y / mag * maxChangeSpeed; change_z = change_z / mag * maxChangeSpeed; @@ -1105,25 +1105,25 @@ namespace FlaxEngine target.Y = current.Y - change_y; target.Z = current.Z - change_z; - float temp_x = (currentVelocity.X + a * change_x) * deltaTime; - float temp_y = (currentVelocity.Y + a * change_y) * deltaTime; - float temp_z = (currentVelocity.Z + a * change_z) * deltaTime; + Real temp_x = (currentVelocity.X + a * change_x) * deltaTime; + Real temp_y = (currentVelocity.Y + a * change_y) * deltaTime; + Real temp_z = (currentVelocity.Z + a * change_z) * deltaTime; currentVelocity.X = (currentVelocity.X - a * temp_x) * e; currentVelocity.Y = (currentVelocity.Y - a * temp_y) * e; currentVelocity.Z = (currentVelocity.Z - a * temp_z) * e; - float output_x = target.X + (change_x + temp_x) * e; - float output_y = target.Y + (change_y + temp_y) * e; - float output_z = target.Z + (change_z + temp_z) * e; + Real output_x = target.X + (change_x + temp_x) * e; + Real output_y = target.Y + (change_y + temp_y) * e; + Real output_z = target.Z + (change_z + temp_z) * e; - float x1 = originalTo.X - current.X; - float y1 = originalTo.Y - current.Y; - float z1 = originalTo.Z - current.Z; + Real x1 = originalTo.X - current.X; + Real y1 = originalTo.Y - current.Y; + Real z1 = originalTo.Z - current.Z; - float x2 = output_x - originalTo.X; - float y2 = output_y - originalTo.Y; - float z2 = output_z - originalTo.Z; + Real x2 = output_x - originalTo.X; + Real y2 = output_y - originalTo.Y; + Real z2 = output_z - originalTo.Z; if (x1 * x2 + y1 * y2 + z1 * z2 > 0) { From 4e44002259bd10af946b60eab855127dd64526dc Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj <40241148+AndrejStojkovic@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:47:38 +0200 Subject: [PATCH 4/9] Fix SmoothDamp missed type casting for Vector 3 --- Source/Engine/Core/Math/Vector3.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 53ec824fe..d009a692e 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1092,7 +1092,7 @@ namespace FlaxEngine float maxChangeSpeed = maxSpeed * smoothTime; float changeSq = maxChangeSpeed * maxChangeSpeed; - float sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; + Real sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; if (sqrMag > changeSq) { var mag = (Real)Math.Sqrt(sqrMag); From e1f528ec9acb74689d95b169252e6dfdb1dcfcb3 Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj <40241148+AndrejStojkovic@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:48:16 +0200 Subject: [PATCH 5/9] Fix SmoothDamp missed type casting for Vector2 --- Source/Engine/Core/Math/Vector2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index cb0cd17d1..3eea3890f 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -1001,7 +1001,7 @@ namespace FlaxEngine float maxChangeSpeed = maxSpeed * smoothTime; float changeSq = maxChangeSpeed * maxChangeSpeed; - float sqrDist = change_x * change_x + change_y * change_y; + Real sqrDist = change_x * change_x + change_y * change_y; if (sqrDist > changeSq) { var dist = (Real)Math.Sqrt(sqrDist); From 3f299f4cf6aba92760b644b980cc977e4a9295e2 Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Tue, 19 Sep 2023 15:57:33 +0200 Subject: [PATCH 6/9] Just in case change type cast for other variables as well --- Source/Engine/Core/Math/Vector2.cs | 10 +++++----- Source/Engine/Core/Math/Vector3.cs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 3eea3890f..568c51764 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -991,16 +991,16 @@ namespace FlaxEngine public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime) { smoothTime = Mathf.Max(0.0001f, smoothTime); - float a = 2f / smoothTime; - float b = a * deltaTime; - float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); + Real a = 2f / smoothTime; + Real b = a * deltaTime; + Real e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); Real change_x = current.X - target.X; Real change_y = current.Y - target.Y; Vector2 originalTo = target; - float maxChangeSpeed = maxSpeed * smoothTime; - float changeSq = maxChangeSpeed * maxChangeSpeed; + Real maxChangeSpeed = maxSpeed * smoothTime; + Real changeSq = maxChangeSpeed * maxChangeSpeed; Real sqrDist = change_x * change_x + change_y * change_y; if (sqrDist > changeSq) { diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index d009a692e..0ad557a32 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1080,9 +1080,9 @@ namespace FlaxEngine public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime) { smoothTime = Mathf.Max(0.0001f, smoothTime); - float a = 2f / smoothTime; - float b = a * deltaTime; - float e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); + Real a = 2f / smoothTime; + Real b = a * deltaTime; + Real e = 1f / (1f + b + 0.48f * b * b + 0.235f * b * b * b); Real change_x = current.X - target.X; Real change_y = current.Y - target.Y; @@ -1090,8 +1090,8 @@ namespace FlaxEngine Vector3 originalTo = target; - float maxChangeSpeed = maxSpeed * smoothTime; - float changeSq = maxChangeSpeed * maxChangeSpeed; + Real maxChangeSpeed = maxSpeed * smoothTime; + Real changeSq = maxChangeSpeed * maxChangeSpeed; Real sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; if (sqrMag > changeSq) { From 8930c7ba565def87f8c618d7359dfb036eb97ad4 Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Tue, 19 Sep 2023 16:09:18 +0200 Subject: [PATCH 7/9] Implemented static functions for length and length squared for Vectors --- Source/Engine/Core/Math/Vector2.cs | 18 ++++++++++++++++++ Source/Engine/Core/Math/Vector3.cs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 568c51764..e953acfd6 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -821,6 +821,24 @@ namespace FlaxEngine return value; } + /// + /// Returns the length of a vector. + /// + /// The vector to get the length from. + public static Real Length(Vector2 vector) + { + return (Real)Mathf.Sqrt(vector.X * vector.X + vector.Y * vector.Y); + } + + /// + /// Returns the length squared of a vector. + /// + /// The vector to get the length squared from. + public static Real LengthSquared(Vector2 vector) + { + return vector.X * vector.X + vector.Y * vector.Y; + } + /// /// Makes sure that Length of the output vector is always below max and above 0. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 0ad557a32..c4bcf6979 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -963,6 +963,24 @@ namespace FlaxEngine return value; } + /// + /// Returns the length of a vector. + /// + /// The vector to get the length from. + public static Real Length(Vector3 vector) + { + return (Real)Mathf.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z); + } + + /// + /// Returns the length squared of a vector. + /// + /// The vector to get the length squared from. + public static Real LengthSquared(Vector3 vector) + { + return vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z; + } + /// /// Makes sure that Length of the output vector is always below max and above 0. /// From cb460af2646090718b7bb30c6df652ba91f3d139 Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Tue, 19 Sep 2023 19:29:59 +0200 Subject: [PATCH 8/9] Revert last feature since feature already exists --- Source/Engine/Core/Math/Vector2.cs | 18 ------------------ Source/Engine/Core/Math/Vector3.cs | 18 ------------------ 2 files changed, 36 deletions(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index e953acfd6..568c51764 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -821,24 +821,6 @@ namespace FlaxEngine return value; } - /// - /// Returns the length of a vector. - /// - /// The vector to get the length from. - public static Real Length(Vector2 vector) - { - return (Real)Mathf.Sqrt(vector.X * vector.X + vector.Y * vector.Y); - } - - /// - /// Returns the length squared of a vector. - /// - /// The vector to get the length squared from. - public static Real LengthSquared(Vector2 vector) - { - return vector.X * vector.X + vector.Y * vector.Y; - } - /// /// Makes sure that Length of the output vector is always below max and above 0. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index c4bcf6979..0ad557a32 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -963,24 +963,6 @@ namespace FlaxEngine return value; } - /// - /// Returns the length of a vector. - /// - /// The vector to get the length from. - public static Real Length(Vector3 vector) - { - return (Real)Mathf.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z); - } - - /// - /// Returns the length squared of a vector. - /// - /// The vector to get the length squared from. - public static Real LengthSquared(Vector3 vector) - { - return vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z; - } - /// /// Makes sure that Length of the output vector is always below max and above 0. /// From 35ee890f9fcc725ec2188ce4ea554d853eaf758e Mon Sep 17 00:00:00 2001 From: Andrej Stojkovikj Date: Tue, 19 Sep 2023 19:33:25 +0200 Subject: [PATCH 9/9] Fix in typo, should be length instead of magnitude --- Source/Engine/Core/Math/Vector3.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 0ad557a32..59d1f63db 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1092,13 +1092,13 @@ namespace FlaxEngine Real maxChangeSpeed = maxSpeed * smoothTime; Real changeSq = maxChangeSpeed * maxChangeSpeed; - Real sqrMag = change_x * change_x + change_y * change_y + change_z * change_z; - if (sqrMag > changeSq) + Real sqrLen = change_x * change_x + change_y * change_y + change_z * change_z; + if (sqrLen > changeSq) { - var mag = (Real)Math.Sqrt(sqrMag); - change_x = change_x / mag * maxChangeSpeed; - change_y = change_y / mag * maxChangeSpeed; - change_z = change_z / mag * maxChangeSpeed; + var len = (Real)Math.Sqrt(sqrLen); + change_x = change_x / len * maxChangeSpeed; + change_y = change_y / len * maxChangeSpeed; + change_z = change_z / len * maxChangeSpeed; } target.X = current.X - change_x;