From 17fe000a1e8aac9b74b9d1b2a2cc25bdd1eef739 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sun, 21 Feb 2021 20:06:39 +0100 Subject: [PATCH] Fix math tests due to angle/direction changes. --- Robust.Shared.Maths/Angle.cs | 2 +- Robust.Shared.Maths/Direction.cs | 13 ++++--- Robust.UnitTesting/Shared/Maths/Angle_Test.cs | 9 ----- .../Shared/Maths/Direction_Test.cs | 35 ++++++++++++------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Robust.Shared.Maths/Angle.cs b/Robust.Shared.Maths/Angle.cs index 6c29f7d2c..4d9ca8cf5 100644 --- a/Robust.Shared.Maths/Angle.cs +++ b/Robust.Shared.Maths/Angle.cs @@ -45,7 +45,7 @@ namespace Robust.Shared.Maths public static Angle FromWorldVec(Vector2 dir) { - return new Angle(dir) + MathHelper.PiOver2; + return new Angle(dir) + Math.PI / 2; } /// diff --git a/Robust.Shared.Maths/Direction.cs b/Robust.Shared.Maths/Direction.cs index d2ebddad7..294c0b793 100644 --- a/Robust.Shared.Maths/Direction.cs +++ b/Robust.Shared.Maths/Direction.cs @@ -86,14 +86,14 @@ namespace Robust.Shared.Maths private static Vector2[] directionVectors = new[] { + new Vector2(0, -1), + new Vector2(1, -1).Normalized, new Vector2(1, 0), new Vector2(1, 1).Normalized, new Vector2(0, 1), new Vector2(-1, 1).Normalized, new Vector2(-1, 0), - new Vector2(-1, -1).Normalized, - new Vector2(0, -1), - new Vector2(1, -1).Normalized + new Vector2(-1, -1).Normalized }; /// /// Converts a Direction to a normalized Direction vector. @@ -112,7 +112,12 @@ namespace Robust.Shared.Maths /// Angle of the vector. public static Angle ToAngle(this Vector2 vec) { - return Math.Atan2(vec.Y, vec.X); + return new(vec); + } + + public static Angle ToWorldAngle(this Vector2 vec) + { + return Angle.FromWorldVec(vec); } } } diff --git a/Robust.UnitTesting/Shared/Maths/Angle_Test.cs b/Robust.UnitTesting/Shared/Maths/Angle_Test.cs index 9df008986..62a84e72f 100644 --- a/Robust.UnitTesting/Shared/Maths/Angle_Test.cs +++ b/Robust.UnitTesting/Shared/Maths/Angle_Test.cs @@ -124,15 +124,6 @@ namespace Robust.UnitTesting.Shared.Maths Assert.That((control - target.ToVec()).LengthSquared, Is.AtMost(Epsilon)); } - [Test] - [Sequential] - public void TestAngleToDirection([ValueSource(nameof(Sources))] (float, float, Direction, double) test) - { - var target = new Angle(test.Item4); - - Assert.That(target.GetDir(), Is.EqualTo(test.Item3)); - } - [Test] [TestCase(MathHelper.PiOver2, ExpectedResult = Direction.East)] [TestCase(0, ExpectedResult = Direction.South)] diff --git a/Robust.UnitTesting/Shared/Maths/Direction_Test.cs b/Robust.UnitTesting/Shared/Maths/Direction_Test.cs index 853e3d447..3e472087d 100644 --- a/Robust.UnitTesting/Shared/Maths/Direction_Test.cs +++ b/Robust.UnitTesting/Shared/Maths/Direction_Test.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Robust.Shared.Maths; using NUnit.Framework; @@ -12,14 +13,14 @@ namespace Robust.UnitTesting.Shared.Maths private static IEnumerable<(float x, float y, Direction dir, double angle)> sources => new(float, float, Direction, double)[] { - (1, 0, Direction.East, 0.0), - (1, 1, Direction.NorthEast, System.Math.PI / 4.0), - (0, 1, Direction.North, System.Math.PI / 2.0), - (-1, 1, Direction.NorthWest, 3 * System.Math.PI / 4.0), - (-1, 0, Direction.West, System.Math.PI), - (-1, -1, Direction.SouthWest, -3 * System.Math.PI / 4.0), - (0, -1, Direction.South, -System.Math.PI / 2.0), - (1, -1, Direction.SouthEast, -System.Math.PI / 4.0) + (1, 0, Direction.East, Angle.FromDegrees(90)), + (1, 1, Direction.NorthEast, Angle.FromDegrees(135)), + (0, 1, Direction.North, Angle.FromDegrees(180)), + (-1, 1, Direction.NorthWest, Angle.FromDegrees(-135)), + (-1, 0, Direction.West, Angle.FromDegrees(-90)), + (-1, -1, Direction.SouthWest, Angle.FromDegrees(-45)), + (0, -1, Direction.South, 0), + (1, -1, Direction.SouthEast, Angle.FromDegrees(45)) }; [Test] @@ -29,7 +30,7 @@ namespace Robust.UnitTesting.Shared.Maths var control = test.Item4; var val = test.Item3.ToAngle(); - Assert.That(System.Math.Abs(control - val), Is.AtMost(Epsilon)); + Assert.That(val.Theta, Is.EqualTo(control).Within(Epsilon)); } [Test] @@ -39,7 +40,7 @@ namespace Robust.UnitTesting.Shared.Maths var control = new Vector2(test.Item1, test.Item2).Normalized; var val = test.Item3.ToVec(); - Assert.That((control - val).LengthSquared, Is.AtMost(Epsilon)); + Assert.That(val, Is.Approximately(control)); } [Test] @@ -48,7 +49,7 @@ namespace Robust.UnitTesting.Shared.Maths { var target = new Vector2(test.Item1, test.Item2).Normalized; - Assert.That(System.Math.Abs(target.ToAngle() - test.Item4), Is.AtMost(Epsilon)); + Assert.That(target.ToWorldAngle().Reduced(), Is.Approximately(new Angle(test.Item4))); } [Test] @@ -59,5 +60,15 @@ namespace Robust.UnitTesting.Shared.Maths Assert.That(target.GetDir(), Is.EqualTo(test.Item3)); } + + [Test] + [Sequential] + public void TestAngleToDirection([ValueSource(nameof(sources))] (float, float, Direction, double) test) + { + var target = new Angle(test.Item4); + + Assert.That(target.GetDir(), Is.EqualTo(test.Item3)); + } + } }