Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Sep 25, 2024
1 parent fecfaf4 commit 13ed9c9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 44 deletions.
11 changes: 1 addition & 10 deletions Robust.Shared.Maths/Matrix3Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,13 @@ public static Box2 TransformBox(this Matrix3x2 refFromBox, in Box2 box)
return Unsafe.As<Vector128<float>, Box2>(ref lbrt);
}

/// <summary>
/// Gets the position of the Matrix. Will have some precision loss.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Position(this Matrix3x2 t)
{
return new Vector2(t.M31, t.M32);
}

/// <summary>
/// Gets the rotation of the Matrix. Will have some precision loss.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Angle Rotation(this Matrix3x2 t)
{
return new Vector2(t.M11, t.M12).ToAngle();
return new Angle(Math.Atan2(t.M12, t.M11));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
12 changes: 5 additions & 7 deletions Robust.Shared/Physics/B2DynamicTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,15 +969,13 @@ internal void RayCastNew(RayCastInput input, long mask, ref WorldRayCastContext

var stack = new GrowableStack<Proxy>(stackalloc Proxy[256]);
ref var baseRef = ref _nodes[0];
var stackCount = 1;
stack.Push(_root);

var subInput = input;

while (stackCount > 0)
while (stack.GetCount() > 0)
{
var nodeId = stack.Pop();
stackCount = stack.GetCount();

if (nodeId == Proxy.Free)
{
Expand Down Expand Up @@ -1025,7 +1023,8 @@ internal void RayCastNew(RayCastInput input, long mask, ref WorldRayCastContext
}
}
else
{
{
var stackCount = stack.GetCount();
Assert( stackCount < 256 - 1 );
if (stackCount < 256 - 1 )
{
Expand Down Expand Up @@ -1090,13 +1089,11 @@ internal void ShapeCast(ShapeCastInput input, long maskBits, TreeShapeCastCallba

ref var baseRef = ref _nodes[0];
var stack = new GrowableStack<Proxy>(stackalloc Proxy[256]);
var stackCount = 1;
stack.Push(_root);

while (stackCount > 0)
while (stack.GetCount() > 0)
{
var nodeId = stack.Pop();
stackCount = stack.GetCount();

if (nodeId == Proxy.Free)
{
Expand Down Expand Up @@ -1144,6 +1141,7 @@ internal void ShapeCast(ShapeCastInput input, long maskBits, TreeShapeCastCallba
}
else
{
var stackCount = stack.GetCount();
Assert(stackCount < 256 - 1);

if (stackCount < 255)
Expand Down
10 changes: 10 additions & 0 deletions Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ private static float RayCastCallback(RayCastInput input, FixtureProxy proxy, ref
return input.MaxFraction;
}

if (worldContext.Filter.IsIgnored?.Invoke(proxy.Entity) == true)
{
return input.MaxFraction;
}

var transform = worldContext.Physics.GetLocalPhysicsTransform(proxy.Entity);
var output = worldContext.System.RayCastShape(input, proxy.Fixture.Shape, transform);

Expand Down Expand Up @@ -402,6 +407,11 @@ private float ShapeCastCallback(ShapeCastInput input, FixtureProxy proxy, ref Wo
return input.MaxFraction;
}

if (worldContext.Filter.IsIgnored?.Invoke(proxy.Entity) == true)
{
return input.MaxFraction;
}

var transform = worldContext.Physics.GetLocalPhysicsTransform(proxy.Entity);
var output = ShapeCastShape(input, proxy.Fixture.Shape, transform);

Expand Down
5 changes: 5 additions & 0 deletions Robust.Shared/Physics/Systems/RayCastSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ public record struct QueryFilter
/// query would accept for collision.
/// </summary>
public long MaskBits;

/// <summary>
/// Return whether to ignore an entity.
/// </summary>
public Func<EntityUid, bool>? IsIgnored;
}

/// Prototype callback for ray casts.
Expand Down
8 changes: 8 additions & 0 deletions Robust.UnitTesting/Shared/Maths/Angle_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public sealed class Angle_Test
(0.92387953251128674f, -0.38268343236508978f, Direction.East, -System.Math.PI / 8.0)
};

[Test]
public void TestAngleDegrees()
{
const double degrees = 75d;
var angle = Angle.FromDegrees(degrees);
Assert.That(angle.Degrees, Is.EqualTo(degrees));
}

[Test]
public void TestAngleZero()
{
Expand Down
21 changes: 4 additions & 17 deletions Robust.UnitTesting/Shared/Maths/Matrix3_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,17 @@ namespace Robust.UnitTesting.Shared.Maths
[TestOf(typeof(Matrix3x2))]
public sealed class Matrix3_Test
{
private static readonly TestCaseData[] Positions = new TestCaseData[]
{
new(Matrix3x2.Identity, Vector2.Zero),
new(Matrix3x2.CreateTranslation(Vector2.One), Vector2.One),
new(Matrix3x2.CreateTranslation(new Vector2(1f, 0f)), new Vector2(1f, 0f)),
};

private static readonly TestCaseData[] Rotations = new TestCaseData[]
{
new(Matrix3x2.Identity, Angle.Zero),
new(Matrix3x2.CreateRotation(MathF.PI / 2), new Angle(MathF.PI / 2)),
new(Matrix3x2.CreateRotation(MathF.PI), Math.PI),
new(Matrix3x2.CreateRotation(MathF.PI / 2f), new Angle(Math.PI / 2)),
new(Matrix3x2.CreateRotation(MathF.PI), new Angle(Math.PI)),
};

[Test, TestCaseSource(nameof(Positions))]
public void GetPositionTest(Matrix3x2 matrix, Vector2 vec)
{
Assert.That(matrix.Position(), Is.EqualTo(vec));
}

[Test, TestCaseSource(nameof(Rotations))]
public void GetRotationTest(Matrix3x2 matrix, Angle angle)
{
Assert.That(matrix.Rotation(), Is.EqualTo(angle));
Assert.That(angle, Is.EqualTo(matrix.Rotation()));
}

[Test]
Expand All @@ -47,7 +34,7 @@ public void TranslationTest()
var origin = new Vector2(0, 0);
var result = Vector2.Transform(origin, matrix);

Assert.That(control == result, Is.True, result.ToString);
Assert.That(control, Is.EqualTo(result), result.ToString);
}

private static readonly IEnumerable<(Vector2, double)> _rotationTests = new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System;
using System.Globalization;
using NUnit.Framework;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager;
Expand All @@ -11,24 +12,46 @@ namespace Robust.UnitTesting.Shared.Serialization.TypeSerializers
[TestOf(typeof(AngleSerializer))]
public sealed class AngleSerializerTest : SerializationTest
{
[Test]
public void SerializationTest()
private static readonly TestCaseData[] _source = new[]
{
var degrees = 75d;
var angle = Angle.FromDegrees(degrees);
new TestCaseData(Math.PI),
new TestCaseData(Math.PI / 2),
new TestCaseData(Math.PI / 4),
new TestCaseData(0.515),
new TestCaseData(75),
};

[Test, TestCaseSource(nameof(_source))]
public void SerializationRadsTest(double radians)
{
var angle = new Angle(radians);
var node = Serialization.WriteValueAs<ValueDataNode>(angle);
var serializedValue = $"{MathHelper.DegreesToRadians(degrees).ToString(CultureInfo.InvariantCulture)} rad";
var serializedValue = $"{radians.ToString(CultureInfo.InvariantCulture)} rad";

Assert.That(node.Value, Is.EqualTo(serializedValue));
}

[Test]
public void DeserializationTest()
[Test, TestCaseSource(nameof(_source))]
public void DeserializationRadsTest(double radians)
{
var degrees = 75;
var node = new ValueDataNode(degrees.ToString());
var angle = new Angle(radians);
var node = new ValueDataNode($"{radians.ToString(CultureInfo.InvariantCulture)} rad");
var deserializedAngle = Serialization.Read<Angle>(node);

Assert.That(deserializedAngle, Is.EqualTo(angle));
}

/*
* Serialization of degrees test won't work because it's comparing degrees to radians.
*/

[Test, TestCaseSource(nameof(_source))]
public void DeserializationDegreesTest(double radians)
{
var degrees = MathHelper.RadiansToDegrees(radians);
var angle = Angle.FromDegrees(degrees);
var node = new ValueDataNode($"{degrees.ToString(CultureInfo.InvariantCulture)}");
var deserializedAngle = Serialization.Read<Angle>(node);

Assert.That(deserializedAngle, Is.EqualTo(angle));
}
Expand Down

0 comments on commit 13ed9c9

Please sign in to comment.