Fix math tests due to angle/direction changes.

This commit is contained in:
Pieter-Jan Briers
2021-02-21 20:06:39 +01:00
parent fba415e765
commit 17fe000a1e
4 changed files with 33 additions and 26 deletions

View File

@@ -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;
}
/// <summary>

View File

@@ -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
};
/// <summary>
/// Converts a Direction to a normalized Direction vector.
@@ -112,7 +112,12 @@ namespace Robust.Shared.Maths
/// <returns>Angle of the vector.</returns>
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);
}
}
}

View File

@@ -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)]

View File

@@ -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));
}
}
}