From 49cb9985eeed5e9fdf7297562632711625e55e59 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Mon, 31 Mar 2025 23:39:12 -0400 Subject: [PATCH 01/45] Set position after setting velocity --- .../Physics/Systems/SharedPhysicsSystem.Island.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Island.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Island.cs index b3fbc3b344c..3be137287ed 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Island.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Island.cs @@ -1026,12 +1026,6 @@ private void UpdateBodies( var angle = angles[offset + i]; var xform = xformQuery.GetComponent(uid); - // Temporary NaN guards until PVS is fixed. - if (!float.IsNaN(position.X) && !float.IsNaN(position.Y)) - { - _transform.SetLocalPositionRotation(xform, xform.LocalPosition + position, xform.LocalRotation + angle); - } - var linVelocity = linearVelocities[offset + i]; var physicsDirtied = false; @@ -1047,6 +1041,13 @@ private void UpdateBodies( physicsDirtied |= SetAngularVelocity(uid, angVelocity, false, body: body); } + // Temporary NaN guards until PVS is fixed. + // May reparent object and change body's velocity. + if (!float.IsNaN(position.X) && !float.IsNaN(position.Y)) + { + _transform.SetLocalPositionRotation(xform, xform.LocalPosition + position, xform.LocalRotation + angle); + } + if (physicsDirtied) Dirty(uid, body); } From 3c754a4f492ec7a8b448be52b3650d3d8a9bf751 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Tue, 1 Apr 2025 15:04:05 +1100 Subject: [PATCH 02/45] Don't disable contacting collisionwake ents (#5798) Good for some content stuff I don't think it caused issues. --- Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs b/Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs index e5549ade371..8b181b1fa8c 100644 --- a/Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs +++ b/Robust.Shared/GameObjects/Systems/CollisionWakeSystem.cs @@ -96,6 +96,7 @@ internal void UpdateCanCollide( // If we're attached to the map we'll also just never disable collision due to how grid movement works. var canCollide = body.Awake || + body.ContactCount > 0 || (TryComp(uid, out JointComponent? jointComponent) && jointComponent.JointCount > 0) || xform.GridUid == null; From ae625ebad8d894a2ecd387a79dd3ef403577adf3 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 2 Apr 2025 00:04:51 +1100 Subject: [PATCH 03/45] Inline manifold points (#5794) * Inline manifold points Max is 2 so no reason to use an array over a fixedarray. * this --- .../Collision/CollisionManager.Circles.cs | 2 +- .../Collision/CollisionManager.EdgeCircle.cs | 18 ++--- .../Collision/CollisionManager.EdgePolygon.cs | 8 +-- .../CollisionManager.PolygonCircle.cs | 8 +-- .../Collision/CollisionManager.Polygons.cs | 4 +- .../Physics/Collision/CollisionManager.cs | 13 ++-- Robust.Shared/Physics/Collision/Manifold.cs | 28 ++++++-- .../Physics/Dynamics/Contacts/Contact.cs | 9 ++- .../Contacts/ContactPositionConstraint.cs | 3 +- .../Contacts/ContactVelocityConstraint.cs | 7 +- .../Systems/SharedPhysicsSystem.Contacts.cs | 5 +- .../Systems/SharedPhysicsSystem.Solver.cs | 56 ++++++++------- Robust.Shared/Utility/FixedArray.cs | 68 +++++++++++++++---- .../Shared/Physics/ManifoldManager_Test.cs | 27 +++++--- 14 files changed, 170 insertions(+), 86 deletions(-) diff --git a/Robust.Shared/Physics/Collision/CollisionManager.Circles.cs b/Robust.Shared/Physics/Collision/CollisionManager.Circles.cs index 864c3bd103d..ac541b08a8d 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.Circles.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.Circles.cs @@ -27,7 +27,7 @@ public void CollideCircles(ref Manifold manifold, PhysShapeCircle circleA, in Tr manifold.LocalNormal = Vector2.Zero; manifold.PointCount = 1; - ref var p0 = ref manifold.Points[0]; + ref var p0 = ref manifold.Points._00; p0.LocalPoint = Vector2.Zero; // Also here p0.Id.Key = 0; diff --git a/Robust.Shared/Physics/Collision/CollisionManager.EdgeCircle.cs b/Robust.Shared/Physics/Collision/CollisionManager.EdgeCircle.cs index 5cd26d5bd31..71952029014 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.EdgeCircle.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.EdgeCircle.cs @@ -79,9 +79,9 @@ public void CollideEdgeAndCircle(ref Manifold manifold, EdgeShape edgeA, in Tran manifold.Type = ManifoldType.Circles; manifold.LocalNormal = Vector2.Zero; manifold.LocalPoint = P; - manifold.Points[0].Id.Key = 0; - manifold.Points[0].Id.Features = cf; - manifold.Points[0].LocalPoint = circleB.Position; + manifold.Points._00.Id.Key = 0; + manifold.Points._00.Id.Features = cf; + manifold.Points._00.LocalPoint = circleB.Position; return; } @@ -114,9 +114,9 @@ public void CollideEdgeAndCircle(ref Manifold manifold, EdgeShape edgeA, in Tran manifold.Type = ManifoldType.Circles; manifold.LocalNormal = Vector2.Zero; manifold.LocalPoint = P; - manifold.Points[0].Id.Key = 0; - manifold.Points[0].Id.Features = cf; - manifold.Points[0].LocalPoint = circleB.Position; + manifold.Points._00.Id.Key = 0; + manifold.Points._00.Id.Features = cf; + manifold.Points._00.LocalPoint = circleB.Position; return; } @@ -142,8 +142,8 @@ public void CollideEdgeAndCircle(ref Manifold manifold, EdgeShape edgeA, in Tran manifold.Type = ManifoldType.FaceA; manifold.LocalNormal = n; manifold.LocalPoint = A; - manifold.Points[0].Id.Key = 0; - manifold.Points[0].Id.Features = cf; - manifold.Points[0].LocalPoint = circleB.Position; + manifold.Points._00.Id.Key = 0; + manifold.Points._00.Id.Features = cf; + manifold.Points._00.LocalPoint = circleB.Position; } } diff --git a/Robust.Shared/Physics/Collision/CollisionManager.EdgePolygon.cs b/Robust.Shared/Physics/Collision/CollisionManager.EdgePolygon.cs index cc583beb311..7a3473cb142 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.EdgePolygon.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.EdgePolygon.cs @@ -238,15 +238,15 @@ public void CollideEdgeAndPolygon(ref Manifold manifold, EdgeShape edgeA, in Tra } var pointCount = 0; + var points = manifold.Points.AsSpan; + for (var i = 0; i < 2; ++i) { - float separation; - - separation = Vector2.Dot(refFace.normal, clipPoints2[i].V - refFace.v1); + var separation = Vector2.Dot(refFace.normal, clipPoints2[i].V - refFace.v1); if (separation <= radius) { - ref var cp = ref manifold.Points[pointCount]; + ref var cp = ref points[pointCount]; if (primaryAxis.Type == EPAxisType.EdgeA) { diff --git a/Robust.Shared/Physics/Collision/CollisionManager.PolygonCircle.cs b/Robust.Shared/Physics/Collision/CollisionManager.PolygonCircle.cs index efca280c4d0..a032ef5be1b 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.PolygonCircle.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.PolygonCircle.cs @@ -64,7 +64,7 @@ public void CollidePolygonAndCircle(ref Manifold manifold, PolygonShape polygonA manifold.LocalNormal = normals[normalIndex]; manifold.LocalPoint = (v1 + v2) * 0.5f; - ref var p0 = ref manifold.Points[0]; + ref var p0 = ref manifold.Points._00; p0.LocalPoint = circleB.Position; p0.Id.Key = 0; @@ -88,7 +88,7 @@ public void CollidePolygonAndCircle(ref Manifold manifold, PolygonShape polygonA manifold.LocalNormal = (cLocal - v1).Normalized(); manifold.LocalPoint = v1; - ref var p0 = ref manifold.Points[0]; + ref var p0 = ref manifold.Points._00; p0.LocalPoint = circleB.Position; p0.Id.Key = 0; @@ -107,7 +107,7 @@ public void CollidePolygonAndCircle(ref Manifold manifold, PolygonShape polygonA manifold.LocalNormal = (cLocal - v2).Normalized(); manifold.LocalPoint = v2; - ref var p0 = ref manifold.Points[0]; + ref var p0 = ref manifold.Points._00; p0.LocalPoint = circleB.Position; p0.Id.Key = 0; @@ -126,7 +126,7 @@ public void CollidePolygonAndCircle(ref Manifold manifold, PolygonShape polygonA manifold.LocalNormal = normals[vertIndex1]; manifold.LocalPoint = faceCenter; - ref var p0 = ref manifold.Points[0]; + ref var p0 = ref manifold.Points._00; p0.LocalPoint = circleB.Position; p0.Id.Key = 0; diff --git a/Robust.Shared/Physics/Collision/CollisionManager.Polygons.cs b/Robust.Shared/Physics/Collision/CollisionManager.Polygons.cs index 44b95d99de3..69d9bc1ee9a 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.Polygons.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.Polygons.cs @@ -238,6 +238,8 @@ public void CollidePolygons(ref Manifold manifold, PolygonShape polyA, in Transf manifold.LocalPoint = planePoint; int pointCount = 0; + var points = manifold.Points.AsSpan; + for (int i = 0; i < 2; ++i) { Vector2 value = clipPoints2[i].V; @@ -245,7 +247,7 @@ public void CollidePolygons(ref Manifold manifold, PolygonShape polyA, in Transf if (separation <= totalRadius) { - ref var cp = ref manifold.Points[pointCount]; + ref var cp = ref points[pointCount]; cp.LocalPoint = Transform.MulT(xf2, clipPoints2[i].V); cp.Id = clipPoints2[i].ID; diff --git a/Robust.Shared/Physics/Collision/CollisionManager.cs b/Robust.Shared/Physics/Collision/CollisionManager.cs index c5584ed3bf6..7161bc582f9 100644 --- a/Robust.Shared/Physics/Collision/CollisionManager.cs +++ b/Robust.Shared/Physics/Collision/CollisionManager.cs @@ -51,15 +51,18 @@ public static void GetPointStates(ref PointState[] state1, ref PointState[] stat in Manifold manifold2) { // Detect persists and removes. + var points1 = manifold1.Points.AsSpan; + var points2 = manifold2.Points.AsSpan; + for (int i = 0; i < manifold1.PointCount; ++i) { - ContactID id = manifold1.Points[i].Id; + var id = points1[i].Id; state1[i] = PointState.Remove; for (int j = 0; j < manifold2.PointCount; ++j) { - if (manifold2.Points[j].Id.Key == id.Key) + if (points2[j].Id.Key == id.Key) { state1[i] = PointState.Persist; break; @@ -70,13 +73,13 @@ public static void GetPointStates(ref PointState[] state1, ref PointState[] stat // Detect persists and adds. for (int i = 0; i < manifold2.PointCount; ++i) { - ContactID id = manifold2.Points[i].Id; + var id = points2[i].Id; state2[i] = PointState.Add; - for (int j = 0; j < manifold1.PointCount; ++j) + for (var j = 0; j < manifold1.PointCount; ++j) { - if (manifold1.Points[j].Id.Key == id.Key) + if (points1[j].Id.Key == id.Key) { state2[i] = PointState.Persist; break; diff --git a/Robust.Shared/Physics/Collision/Manifold.cs b/Robust.Shared/Physics/Collision/Manifold.cs index 75cd6135fb8..ea6848f7dbe 100644 --- a/Robust.Shared/Physics/Collision/Manifold.cs +++ b/Robust.Shared/Physics/Collision/Manifold.cs @@ -25,6 +25,7 @@ using System.Runtime.InteropServices; using Robust.Shared.Localization; using Robust.Shared.Maths; +using Robust.Shared.Utility; namespace Robust.Shared.Physics.Collision; @@ -146,7 +147,7 @@ public struct Manifold : IEquatable, IApproxEquatable /// /// Points of contact, can only be 0 -> 2. /// - internal ManifoldPoint[] Points; + internal FixedArray2 Points; public ManifoldType Type; @@ -157,9 +158,12 @@ public bool Equals(Manifold other) LocalNormal.Equals(other.LocalNormal) && LocalPoint.Equals(other.LocalPoint))) return false; + var points = Points.AsSpan; + var otherPoints = other.Points.AsSpan; + for (var i = 0; i < PointCount; i++) { - if (!Points[i].Equals(other.Points[i])) return false; + if (!points[i].Equals(otherPoints[i])) return false; } return true; @@ -172,9 +176,12 @@ public bool EqualsApprox(Manifold other) LocalNormal.EqualsApprox(other.LocalNormal) && LocalPoint.EqualsApprox(other.LocalPoint))) return false; + var points = Points.AsSpan; + var otherPoints = other.Points.AsSpan; + for (var i = 0; i < PointCount; i++) { - if (!Points[i].EqualsApprox(other.Points[i])) return false; + if (!points[i].EqualsApprox(otherPoints[i])) return false; } return true; @@ -187,13 +194,26 @@ public bool EqualsApprox(Manifold other, double tolerance) LocalNormal.EqualsApprox(other.LocalNormal, tolerance) && LocalPoint.EqualsApprox(other.LocalPoint, tolerance))) return false; + var points = Points.AsSpan; + var otherPoints = other.Points.AsSpan; + for (var i = 0; i < PointCount; i++) { - if (!Points[i].EqualsApprox(other.Points[i], tolerance)) return false; + if (!points[i].EqualsApprox(otherPoints[i], tolerance)) return false; } return true; } + + public override bool Equals(object? obj) + { + return obj is Manifold manifold && Equals(manifold); + } + + public override int GetHashCode() + { + return HashCode.Combine(LocalNormal, LocalPoint, PointCount, Points, (int)Type); + } } public struct ManifoldPoint : IEquatable, IApproxEquatable diff --git a/Robust.Shared/Physics/Dynamics/Contacts/Contact.cs b/Robust.Shared/Physics/Dynamics/Contacts/Contact.cs index 4f11831c317..71d9dee98ba 100644 --- a/Robust.Shared/Physics/Dynamics/Contacts/Contact.cs +++ b/Robust.Shared/Physics/Dynamics/Contacts/Contact.cs @@ -206,16 +206,19 @@ internal ContactStatus Update(Transform bodyATransform, Transform bodyBTransform // Match old contact ids to new contact ids and copy the // stored impulses to warm start the solver. + var points = Manifold.Points.AsSpan; + var oldPoints = oldManifold.Points.AsSpan; + for (var i = 0; i < Manifold.PointCount; ++i) { - var mp2 = Manifold.Points[i]; + var mp2 = points[i]; mp2.NormalImpulse = 0.0f; mp2.TangentImpulse = 0.0f; var id2 = mp2.Id; for (var j = 0; j < oldManifold.PointCount; ++j) { - var mp1 = oldManifold.Points[j]; + var mp1 = oldPoints[j]; if (mp1.Id.Key == id2.Key) { @@ -225,7 +228,7 @@ internal ContactStatus Update(Transform bodyATransform, Transform bodyBTransform } } - Manifold.Points[i] = mp2; + points[i] = mp2; } if (touching != wasTouching) diff --git a/Robust.Shared/Physics/Dynamics/Contacts/ContactPositionConstraint.cs b/Robust.Shared/Physics/Dynamics/Contacts/ContactPositionConstraint.cs index 4d96a81801f..f2058b6579c 100644 --- a/Robust.Shared/Physics/Dynamics/Contacts/ContactPositionConstraint.cs +++ b/Robust.Shared/Physics/Dynamics/Contacts/ContactPositionConstraint.cs @@ -22,6 +22,7 @@ using System.Numerics; using Robust.Shared.Physics.Collision; +using Robust.Shared.Utility; namespace Robust.Shared.Physics.Dynamics.Contacts { @@ -37,7 +38,7 @@ internal struct ContactPositionConstraint /// public int IndexB { get; set; } - public Vector2[] LocalPoints; + internal FixedArray2 LocalPoints; public Vector2 LocalNormal; diff --git a/Robust.Shared/Physics/Dynamics/Contacts/ContactVelocityConstraint.cs b/Robust.Shared/Physics/Dynamics/Contacts/ContactVelocityConstraint.cs index a1e876c3bc1..cb6735a248d 100644 --- a/Robust.Shared/Physics/Dynamics/Contacts/ContactVelocityConstraint.cs +++ b/Robust.Shared/Physics/Dynamics/Contacts/ContactVelocityConstraint.cs @@ -21,6 +21,7 @@ */ using System.Numerics; +using Robust.Shared.Utility; namespace Robust.Shared.Physics.Dynamics.Contacts { @@ -39,13 +40,13 @@ internal struct ContactVelocityConstraint public int IndexB; // Use 2 as its the max number of manifold points. - public VelocityConstraintPoint[] Points; + public FixedArray2 Points; public Vector2 Normal; - public System.Numerics.Vector4 NormalMass; + public Vector4 NormalMass; - public System.Numerics.Vector4 K; + public Vector4 K; public float InvMassA; public float InvMassB; diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs index b2087ae7803..04e6abd4bcf 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs @@ -113,10 +113,7 @@ public Contact Create() #if DEBUG contact._debugPhysics = _debugPhysicsSystem; #endif - contact.Manifold = new Manifold - { - Points = new ManifoldPoint[2] - }; + contact.Manifold = new Manifold(); return contact; } diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Solver.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Solver.cs index d1b8d9b3df2..ff7c1bcd2fa 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Solver.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Solver.cs @@ -67,7 +67,6 @@ private void ResetSolver( velocityConstraint.TangentSpeed = contact.TangentSpeed; velocityConstraint.IndexA = bodyA.IslandIndex[island.Index]; velocityConstraint.IndexB = bodyB.IslandIndex[island.Index]; - Array.Resize(ref velocityConstraint.Points, 2); // Don't need to reset point data as it all gets set below. var (invMassA, invMassB) = GetInvMass(bodyA, bodyB); @@ -87,7 +86,6 @@ private void ResetSolver( (positionConstraint.InvMassA, positionConstraint.InvMassB) = (invMassA, invMassB); positionConstraint.LocalCenterA = bodyA.LocalCenter; positionConstraint.LocalCenterB = bodyB.LocalCenter; - Array.Resize(ref positionConstraint.LocalPoints, 2); positionConstraint.InvIA = bodyA.InvI; positionConstraint.InvIB = bodyB.InvI; @@ -97,11 +95,14 @@ private void ResetSolver( positionConstraint.RadiusA = radiusA; positionConstraint.RadiusB = radiusB; positionConstraint.Type = manifold.Type; + var points = manifold.Points.AsSpan; + var posPoints = positionConstraint.LocalPoints.AsSpan; + var velPoints = velocityConstraint.Points.AsSpan; for (var j = 0; j < pointCount; ++j) { - var contactPoint = manifold.Points[j]; - ref var constraintPoint = ref velocityConstraint.Points[j]; + var contactPoint = points[j]; + ref var constraintPoint = ref velPoints[j]; if (_warmStarting) { @@ -120,7 +121,7 @@ private void ResetSolver( constraintPoint.TangentMass = 0.0f; constraintPoint.VelocityBias = 0.0f; - positionConstraint.LocalPoints[j] = contactPoint.LocalPoint; + posPoints[j] = contactPoint.LocalPoint; } } } @@ -220,10 +221,11 @@ private void InitializeVelocityConstraints( velocityConstraint.Normal = normal; int pointCount = velocityConstraint.PointCount; + var velPoints = velocityConstraint.Points.AsSpan; for (int j = 0; j < pointCount; ++j) { - ref var vcp = ref velocityConstraint.Points[j]; + ref var vcp = ref velPoints[j]; vcp.RelativeVelocityA = points[j] - centerA; vcp.RelativeVelocityB = points[j] - centerB; @@ -256,8 +258,8 @@ private void InitializeVelocityConstraints( // If we have two points, then prepare the block solver. if (velocityConstraint.PointCount == 2) { - var vcp1 = velocityConstraint.Points[0]; - var vcp2 = velocityConstraint.Points[1]; + var vcp1 = velocityConstraint.Points._00; + var vcp2 = velocityConstraint.Points._01; var rn1A = Vector2Helpers.Cross(vcp1.RelativeVelocityA, velocityConstraint.Normal); var rn1B = Vector2Helpers.Cross(vcp1.RelativeVelocityB, velocityConstraint.Normal); @@ -299,6 +301,7 @@ private void WarmStart( for (var i = 0; i < island.Contacts.Count; ++i) { var velocityConstraint = velocityConstraints[i]; + var velPoints = velocityConstraint.Points.AsSpan; var indexA = velocityConstraint.IndexA; var indexB = velocityConstraint.IndexB; @@ -318,7 +321,7 @@ private void WarmStart( for (var j = 0; j < pointCount; ++j) { - var constraintPoint = velocityConstraint.Points[j]; + var constraintPoint = velPoints[j]; var P = normal * constraintPoint.NormalImpulse + tangent * constraintPoint.TangentImpulse; angVelocityA -= invIA * Vector2Helpers.Cross(constraintPoint.RelativeVelocityA, P); linVelocityA -= P * invMassA; @@ -386,12 +389,13 @@ private void SolveVelocityConstraints( var friction = velocityConstraint.Friction; DebugTools.Assert(pointCount is 1 or 2); + var velPoints = velocityConstraint.Points.AsSpan; // Solve tangent constraints first because non-penetration is more important // than friction. for (var j = 0; j < pointCount; ++j) { - ref var velConstraintPoint = ref velocityConstraint.Points[j]; + ref var velConstraintPoint = ref velPoints[j]; // Relative velocity at contact var dv = vB + Vector2Helpers.Cross(wB, velConstraintPoint.RelativeVelocityB) - vA - Vector2Helpers.Cross(wA, velConstraintPoint.RelativeVelocityA); @@ -419,7 +423,7 @@ private void SolveVelocityConstraints( // Solve normal constraints if (velocityConstraint.PointCount == 1) { - ref var vcp = ref velocityConstraint.Points[0]; + ref var vcp = ref velocityConstraint.Points._00; // Relative velocity at contact Vector2 dv = vB + Vector2Helpers.Cross(wB, vcp.RelativeVelocityB) - vA - Vector2Helpers.Cross(wA, vcp.RelativeVelocityA); @@ -476,8 +480,8 @@ private void SolveVelocityConstraints( // = A * x + b' // b' = b - A * a; - ref var cp1 = ref velocityConstraint.Points[0]; - ref var cp2 = ref velocityConstraint.Points[1]; + ref var cp1 = ref velocityConstraint.Points._00; + ref var cp2 = ref velocityConstraint.Points._01; Vector2 a = new Vector2(cp1.NormalImpulse, cp2.NormalImpulse); DebugTools.Assert(a.X >= 0.0f && a.Y >= 0.0f); @@ -643,14 +647,16 @@ private void StoreImpulses(in IslandData island, ContactVelocityConstraint[] vel { for (var i = 0; i < island.Contacts.Count; ++i) { - ContactVelocityConstraint velocityConstraint = velocityConstraints[i]; + var velocityConstraint = velocityConstraints[i]; ref var manifold = ref island.Contacts[velocityConstraint.ContactIndex].Manifold; + var manPoints = manifold.Points.AsSpan; + var velPoints = velocityConstraint.Points.AsSpan; for (var j = 0; j < velocityConstraint.PointCount; ++j) { - ref var point = ref manifold.Points[j]; - point.NormalImpulse = velocityConstraint.Points[j].NormalImpulse; - point.TangentImpulse = velocityConstraint.Points[j].TangentImpulse; + ref var point = ref manPoints[j]; + point.NormalImpulse = velPoints[j].NormalImpulse; + point.TangentImpulse = velPoints[j].TangentImpulse; } } } @@ -794,7 +800,7 @@ internal static void InitializeManifold( { normal = new Vector2(1.0f, 0.0f); Vector2 pointA = Physics.Transform.Mul(xfA, manifold.LocalPoint); - Vector2 pointB = Physics.Transform.Mul(xfB, manifold.Points[0].LocalPoint); + Vector2 pointB = Physics.Transform.Mul(xfB, manifold.Points._00.LocalPoint); if ((pointA - pointB).LengthSquared() > float.Epsilon * float.Epsilon) { @@ -812,10 +818,11 @@ internal static void InitializeManifold( { normal = Physics.Transform.Mul(xfA.Quaternion2D, manifold.LocalNormal); Vector2 planePoint = Physics.Transform.Mul(xfA, manifold.LocalPoint); + var manPoints = manifold.Points.AsSpan; for (int i = 0; i < manifold.PointCount; ++i) { - Vector2 clipPoint = Physics.Transform.Mul(xfB, manifold.Points[i].LocalPoint); + Vector2 clipPoint = Physics.Transform.Mul(xfB, manPoints[i].LocalPoint); Vector2 cA = clipPoint + normal * (radiusA - Vector2.Dot(clipPoint - planePoint, normal)); Vector2 cB = clipPoint - normal * radiusB; points[i] = (cA + cB) * 0.5f; @@ -827,10 +834,11 @@ internal static void InitializeManifold( { normal = Physics.Transform.Mul(xfB.Quaternion2D, manifold.LocalNormal); Vector2 planePoint = Physics.Transform.Mul(xfB, manifold.LocalPoint); + var manPoints = manifold.Points.AsSpan; for (int i = 0; i < manifold.PointCount; ++i) { - Vector2 clipPoint = Physics.Transform.Mul(xfA, manifold.Points[i].LocalPoint); + Vector2 clipPoint = Physics.Transform.Mul(xfA, manPoints[i].LocalPoint); Vector2 cB = clipPoint + normal * (radiusB - Vector2.Dot(clipPoint - planePoint, normal)); Vector2 cA = clipPoint - normal * radiusA; points[i] = (cA + cB) * 0.5f; @@ -863,7 +871,7 @@ private static void PositionSolverManifoldInitialize( case ManifoldType.Circles: { Vector2 pointA = Physics.Transform.Mul(xfA, pc.LocalPoint); - Vector2 pointB = Physics.Transform.Mul(xfB, pc.LocalPoints[0]); + Vector2 pointB = Physics.Transform.Mul(xfB, pc.LocalPoints._00); normal = pointB - pointA; //FPE: Fix to handle zero normalization @@ -877,10 +885,11 @@ private static void PositionSolverManifoldInitialize( case ManifoldType.FaceA: { + var pcPoints = pc.LocalPoints.AsSpan; normal = Physics.Transform.Mul(xfA.Quaternion2D, pc.LocalNormal); Vector2 planePoint = Physics.Transform.Mul(xfA, pc.LocalPoint); - Vector2 clipPoint = Physics.Transform.Mul(xfB, pc.LocalPoints[index]); + Vector2 clipPoint = Physics.Transform.Mul(xfB, pcPoints[index]); separation = Vector2.Dot(clipPoint - planePoint, normal) - pc.RadiusA - pc.RadiusB; point = clipPoint; } @@ -888,10 +897,11 @@ private static void PositionSolverManifoldInitialize( case ManifoldType.FaceB: { + var pcPoints = pc.LocalPoints.AsSpan; normal = Physics.Transform.Mul(xfB.Quaternion2D, pc.LocalNormal); Vector2 planePoint = Physics.Transform.Mul(xfB, pc.LocalPoint); - Vector2 clipPoint = Physics.Transform.Mul(xfA, pc.LocalPoints[index]); + Vector2 clipPoint = Physics.Transform.Mul(xfA, pcPoints[index]); separation = Vector2.Dot(clipPoint - planePoint, normal) - pc.RadiusA - pc.RadiusB; point = clipPoint; diff --git a/Robust.Shared/Utility/FixedArray.cs b/Robust.Shared/Utility/FixedArray.cs index 50827d1c565..402ed5f2bda 100644 --- a/Robust.Shared/Utility/FixedArray.cs +++ b/Robust.Shared/Utility/FixedArray.cs @@ -79,12 +79,34 @@ public static Span Alloc128(out FixedArray128 discard) } } - internal struct FixedArray2 + internal struct FixedArray2 : IEquatable> { public T _00; public T _01; public Span AsSpan => MemoryMarshal.CreateSpan(ref _00, 2); + + internal FixedArray2(T x0, T x1) + { + _00 = x0; + _01 = x1; + } + + public bool Equals(FixedArray2 other) + { + return EqualityComparer.Default.Equals(_00, other._00) && + EqualityComparer.Default.Equals(_01, other._01); + } + + public override bool Equals(object? obj) + { + return obj is FixedArray2 other && Equals(other); + } + + public override int GetHashCode() + { + return HashCode.Combine(_00, _01); + } } internal struct FixedArray4 : IEquatable> @@ -96,12 +118,20 @@ internal struct FixedArray4 : IEquatable> public Span AsSpan => MemoryMarshal.CreateSpan(ref _00, 4); + internal FixedArray4(T x0, T x1, T x2, T x3) + { + _00 = x0; + _01 = x1; + _02 = x2; + _03 = x3; + } + public bool Equals(FixedArray4 other) { - return _00?.Equals(other._00) == true && - _01?.Equals(other._01) == true && - _02?.Equals(other._02) == true && - _03?.Equals(other._03) == true; + return EqualityComparer.Default.Equals(_00, other._00) && + EqualityComparer.Default.Equals(_01, other._01) && + EqualityComparer.Default.Equals(_02, other._02) && + EqualityComparer.Default.Equals(_03, other._03); } public override bool Equals(object? obj) @@ -128,16 +158,28 @@ internal struct FixedArray8 : IEquatable> public Span AsSpan => MemoryMarshal.CreateSpan(ref _00, 8); + internal FixedArray8(T x0, T x1, T x2, T x3, T x4, T x5, T x6, T x7) + { + _00 = x0; + _01 = x1; + _02 = x2; + _03 = x3; + _04 = x4; + _05 = x5; + _06 = x6; + _07 = x7; + } + public bool Equals(FixedArray8 other) { - return _00?.Equals(other._00) == true && - _01?.Equals(other._01) == true && - _02?.Equals(other._02) == true && - _03?.Equals(other._03) == true && - _04?.Equals(other._04) == true && - _05?.Equals(other._05) == true && - _06?.Equals(other._06) == true && - _07?.Equals(other._07) == true; + return EqualityComparer.Default.Equals(_00, other._00) && + EqualityComparer.Default.Equals(_01, other._01) && + EqualityComparer.Default.Equals(_02, other._02) && + EqualityComparer.Default.Equals(_03, other._03) && + EqualityComparer.Default.Equals(_04, other._04) && + EqualityComparer.Default.Equals(_05, other._05) && + EqualityComparer.Default.Equals(_06, other._06) && + EqualityComparer.Default.Equals(_07, other._07); } public override bool Equals(object? obj) diff --git a/Robust.UnitTesting/Shared/Physics/ManifoldManager_Test.cs b/Robust.UnitTesting/Shared/Physics/ManifoldManager_Test.cs index e4adecbf6d1..f0fc4d027a3 100644 --- a/Robust.UnitTesting/Shared/Physics/ManifoldManager_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/ManifoldManager_Test.cs @@ -5,6 +5,7 @@ using Robust.Shared.Physics; using Robust.Shared.Physics.Collision; using Robust.Shared.Physics.Collision.Shapes; +using Robust.Shared.Utility; namespace Robust.UnitTesting.Shared.Physics { @@ -76,10 +77,7 @@ public void TestPolyOnPolyManifolds() { var transformB = new Transform(Vector2.One, 0f); var transformA = new Transform(transformB.Position + new Vector2(0.5f, 0.0f), 0f); - var manifold = new Manifold() - { - Points = new ManifoldPoint[2] - }; + var manifold = new Manifold(); var expectedManifold = new Manifold { @@ -87,17 +85,24 @@ public void TestPolyOnPolyManifolds() LocalNormal = new Vector2(-1, 0), LocalPoint = new Vector2(-0.5f, 0), PointCount = 2, - Points = new ManifoldPoint[] - { - new() {LocalPoint = new Vector2(0.5f, -0.5f), Id = new ContactID {Key = 65795}}, - new() {LocalPoint = new Vector2(0.5f, 0.5f), Id = new ContactID {Key = 66051}} - } + Points = new FixedArray2( + new ManifoldPoint + { + LocalPoint = new Vector2(0.5f, -0.5f), + Id = new ContactID {Key = 65795} + }, + new ManifoldPoint + { + LocalPoint = new Vector2(0.5f, 0.5f), + Id = new ContactID {Key = 66051} + } + ) }; _manifoldManager.CollidePolygons(ref manifold, _polyA, transformA, _polyB, transformB); - for (var i = 0; i < manifold.Points.Length; i++) + for (var i = 0; i < manifold.PointCount; i++) { - Assert.That(manifold.Points[i], Is.EqualTo(expectedManifold.Points[i])); + Assert.That(manifold.Points.AsSpan[i], Is.EqualTo(expectedManifold.Points.AsSpan[i])); } Assert.That(manifold, Is.EqualTo(expectedManifold)); From e429ba9da3fb5eccfc5277e11d7cb93ddd0d6c33 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 10:34:17 -0400 Subject: [PATCH 04/45] Add reparent velocity unit tests --- .../Physics/GridReparentVelocity_Test.cs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs new file mode 100644 index 00000000000..32c99a23ba8 --- /dev/null +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -0,0 +1,136 @@ +using System.Numerics; +using System.Reflection; +using System.Threading.Tasks; +using NUnit.Framework; +using Robust.Shared.GameObjects; +using Robust.Shared.Map; +using Robust.Shared.Maths; +using Robust.Shared.Physics; +using Robust.Shared.Physics.Collision.Shapes; +using Robust.Shared.Physics.Components; +using Robust.Shared.Physics.Dynamics; +using Robust.Shared.Physics.Systems; + +namespace Robust.UnitTesting.Shared.Physics; + +[TestFixture, TestOf(typeof(SharedPhysicsSystem))] +public sealed class GridReparentVelocity_Test : RobustIntegrationTest +{ + private static readonly string Prototypes = @" +- type: entity + id: ReparentTestObject + components: + - type: Physics + bodyType: Dynamic +"; + + [Test] + public async Task TestLinearVelocityMoveOffGrid() + { + var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; + var server = StartServer(serverOpts); + + await server.WaitIdleAsync(); + + // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body + var systems = server.ResolveDependency(); + var fixtureSystem = systems.GetEntitySystem(); + var mapManager = server.ResolveDependency(); + var entManager = server.ResolveDependency(); + var physSystem = systems.GetEntitySystem(); + var transformSystem = entManager.EntitySysManager.GetEntitySystem(); + var mapSystem = entManager.EntitySysManager.GetEntitySystem(); + + // Set up entities + EntityUid map = default; + EntityUid grid = default; + EntityUid obj = default; + await server.WaitPost(() => + { + map = server.System().CreateMap(out var mapId); + + // Spawn a grid with one tile. + var grid = mapManager.CreateGridEntity(mapId); + mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1)); + var fixtures = entManager.GetComponent(grid); + + // Spawn our test object in the middle of the grid. + obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); + physSystem.SetCanCollide(obj, true); + }); + + await server.WaitAssertion(() => + { + // Our object should start on the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((grid))); + + // Set the velocity of the grid and our object. + physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)); + physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)); + + // Wait a second to clear the grid + physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + var objPhys = entManager.GetComponent(obj); + Assert.That(objPhys.ParentUid, Is.EqualTo((map))); + Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + }); + } + + [Test] + public async Task TestLinearVelocityMoveOntoGrid() + { + var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; + var server = StartServer(serverOpts); + + await server.WaitIdleAsync(); + + // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body + var systems = server.ResolveDependency(); + var fixtureSystem = systems.GetEntitySystem(); + var mapManager = server.ResolveDependency(); + var entManager = server.ResolveDependency(); + var physSystem = systems.GetEntitySystem(); + var transformSystem = entManager.EntitySysManager.GetEntitySystem(); + var mapSystem = entManager.EntitySysManager.GetEntitySystem(); + + // Set up entities + EntityUid map = default; + EntityUid grid = default; + EntityUid obj = default; + await server.WaitPost(() => + { + map = server.System().CreateMap(out var mapId); + + // Spawn a grid with one tile. + var grid = mapManager.CreateGridEntity(mapId); + mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1)); + var fixtures = entManager.GetComponent(grid); + + // Spawn our test object 1 m off of the middle of the grid in both directions. + obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); + physSystem.SetCanCollide(obj, true); + }); + + await server.WaitAssertion(() => + { + // Assert that we start off the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((map))); + + // Set the velocity of the grid and our object. + physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)); + physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)); + + // Wait a second to move onto the middle of the grid + physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + var objPhys = entManager.GetComponent(obj); + Assert.That(objPhys.ParentUid, Is.EqualTo((grid))); + Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-2.0f, -3.0f))); + }); + } +} From 0637d300ef81b3a765df0ac061babfc232a78689 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 10:37:08 -0400 Subject: [PATCH 05/45] cleanup test imports --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 32c99a23ba8..81831369e1b 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -1,14 +1,11 @@ using System.Numerics; -using System.Reflection; using System.Threading.Tasks; using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Physics; -using Robust.Shared.Physics.Collision.Shapes; using Robust.Shared.Physics.Components; -using Robust.Shared.Physics.Dynamics; using Robust.Shared.Physics.Systems; namespace Robust.UnitTesting.Shared.Physics; From 20806a0e107c75929afdf7a7b01a2b95331a6430 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 10:40:23 -0400 Subject: [PATCH 06/45] fix parent lookup --- .../Shared/Physics/GridReparentVelocity_Test.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 81831369e1b..882758c5870 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -69,9 +69,8 @@ await server.WaitAssertion(() => physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - var objPhys = entManager.GetComponent(obj); - Assert.That(objPhys.ParentUid, Is.EqualTo((map))); - Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((map))); + Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); }); } @@ -124,9 +123,8 @@ await server.WaitAssertion(() => physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - var objPhys = entManager.GetComponent(obj); - Assert.That(objPhys.ParentUid, Is.EqualTo((grid))); - Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((grid))); + Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-2.0f, -3.0f))); }); } From 31bdf0ccb0e0af1ddea19a084487e14c4866388f Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 10:47:25 -0400 Subject: [PATCH 07/45] local grid entity, set up grid UID from entity --- .../Physics/GridReparentVelocity_Test.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 882758c5870..f98671d9a02 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -47,9 +47,9 @@ await server.WaitPost(() => map = server.System().CreateMap(out var mapId); // Spawn a grid with one tile. - var grid = mapManager.CreateGridEntity(mapId); - mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1)); - var fixtures = entManager.GetComponent(grid); + var gridEnt = mapManager.CreateGridEntity(mapId); + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + grid = gridEnt.Owner; // Spawn our test object in the middle of the grid. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); @@ -59,7 +59,7 @@ await server.WaitPost(() => await server.WaitAssertion(() => { // Our object should start on the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((grid))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Set the velocity of the grid and our object. physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)); @@ -69,7 +69,7 @@ await server.WaitAssertion(() => physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((map))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); }); @@ -101,9 +101,9 @@ await server.WaitPost(() => map = server.System().CreateMap(out var mapId); // Spawn a grid with one tile. - var grid = mapManager.CreateGridEntity(mapId); - mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1)); - var fixtures = entManager.GetComponent(grid); + var gridEnt = mapManager.CreateGridEntity(mapId); + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + grid = gridEnt.Owner; // Spawn our test object 1 m off of the middle of the grid in both directions. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); @@ -113,7 +113,7 @@ await server.WaitPost(() => await server.WaitAssertion(() => { // Assert that we start off the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((map))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); // Set the velocity of the grid and our object. physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)); @@ -123,7 +123,7 @@ await server.WaitAssertion(() => physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo((grid))); + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-2.0f, -3.0f))); }); From 45adf34e7ebf06c36f85772c863052cc591a0387 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:02:45 -0400 Subject: [PATCH 08/45] add a fixture to the test object --- .../Shared/Physics/GridReparentVelocity_Test.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index f98671d9a02..ecd493018ba 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -19,6 +19,15 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest components: - type: Physics bodyType: Dynamic + - type: Fixtures + fixtures + fix1: + shape: + !type:PhysShapeAabb + bounds: '-0.1,-0.1,0.1,0.1' + hard: false + mask: + - Impassable "; [Test] @@ -125,7 +134,7 @@ await server.WaitAssertion(() => // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-2.0f, -3.0f))); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); }); } } From d2a37d2e76bd691cd2f483e97c1485702657d4d3 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:04:57 -0400 Subject: [PATCH 09/45] assert that SetLinearVelocity works --- .../Shared/Physics/GridReparentVelocity_Test.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index ecd493018ba..3b3b2e700b0 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -71,8 +71,8 @@ await server.WaitAssertion(() => Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Set the velocity of the grid and our object. - physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)); - physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)); + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); // Wait a second to clear the grid physSystem.Update(1.0f); @@ -125,8 +125,8 @@ await server.WaitAssertion(() => Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); // Set the velocity of the grid and our object. - physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)); - physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)); + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); // Wait a second to move onto the middle of the grid physSystem.Update(1.0f); From f96de5872033c882b55c1fd7b081d57ac6c01df4 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:09:48 -0400 Subject: [PATCH 10/45] Missing colon --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 3b3b2e700b0..ed04ca676db 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -20,7 +20,7 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest - type: Physics bodyType: Dynamic - type: Fixtures - fixtures + fixtures: fix1: shape: !type:PhysShapeAabb From ce9f5cca551149b233beff0e0c5ea244af4bfe22 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:21:34 -0400 Subject: [PATCH 11/45] reparent no collision mask in parent test object --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index ed04ca676db..41b3f3bcb92 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -26,8 +26,6 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest !type:PhysShapeAabb bounds: '-0.1,-0.1,0.1,0.1' hard: false - mask: - - Impassable "; [Test] From 49f6c0e0d3cc5e0f83dffdd5ca641f55d99bb4b8 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:30:07 -0400 Subject: [PATCH 12/45] set grid type to dynamic --- .../Shared/Physics/GridReparentVelocity_Test.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 41b3f3bcb92..5b635a5b654 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -53,8 +53,11 @@ await server.WaitPost(() => { map = server.System().CreateMap(out var mapId); - // Spawn a grid with one tile. + // Spawn a grid with one tile, ensure it's movable var gridEnt = mapManager.CreateGridEntity(mapId); + physSystem.SetCanCollide(gridEnt, true); + physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; @@ -107,8 +110,11 @@ await server.WaitPost(() => { map = server.System().CreateMap(out var mapId); - // Spawn a grid with one tile. + // Spawn a grid with one tile, ensure it's movable var gridEnt = mapManager.CreateGridEntity(mapId); + physSystem.SetCanCollide(gridEnt, true); + physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; From 434832213811f445a3fed56d7d534c42b13f1c4a Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:36:15 -0400 Subject: [PATCH 13/45] Ensure we have no damping --- .../Shared/Physics/GridReparentVelocity_Test.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 5b635a5b654..18f29178fa7 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -57,6 +57,7 @@ await server.WaitPost(() => var gridEnt = mapManager.CreateGridEntity(mapId); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(grid), 0.0f); mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; @@ -64,6 +65,7 @@ await server.WaitPost(() => // Spawn our test object in the middle of the grid. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); physSystem.SetCanCollide(obj, true); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(obj), 0.0f); }); await server.WaitAssertion(() => @@ -110,17 +112,19 @@ await server.WaitPost(() => { map = server.System().CreateMap(out var mapId); - // Spawn a grid with one tile, ensure it's movable + // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. var gridEnt = mapManager.CreateGridEntity(mapId); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(grid), 0.0f); mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; - // Spawn our test object 1 m off of the middle of the grid in both directions. + // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); physSystem.SetCanCollide(obj, true); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(obj), 0.0f); }); await server.WaitAssertion(() => From 3137c6a535c0ecf727e2e566cd9e40aee46216ba Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:37:02 -0400 Subject: [PATCH 14/45] comments --- .../Shared/Physics/GridReparentVelocity_Test.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 18f29178fa7..ba9e508dc5f 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -53,7 +53,7 @@ await server.WaitPost(() => { map = server.System().CreateMap(out var mapId); - // Spawn a grid with one tile, ensure it's movable + // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. var gridEnt = mapManager.CreateGridEntity(mapId); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); @@ -62,7 +62,7 @@ await server.WaitPost(() => mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; - // Spawn our test object in the middle of the grid. + // Spawn our test object in the middle of the grid, ensure it has no damping. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); physSystem.SetCanCollide(obj, true); physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(obj), 0.0f); From 3786cb19825b136181733e7e40a18830096af4bc Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:40:18 -0400 Subject: [PATCH 15/45] Entity vs. EntityUid --- .../Shared/Physics/GridReparentVelocity_Test.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index ba9e508dc5f..b3c53fde0c0 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -57,7 +57,7 @@ await server.WaitPost(() => var gridEnt = mapManager.CreateGridEntity(mapId); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(grid), 0.0f); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; @@ -116,7 +116,7 @@ await server.WaitPost(() => var gridEnt = mapManager.CreateGridEntity(mapId); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(grid), 0.0f); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); grid = gridEnt.Owner; From 20e4b3a0e07a365c51306cba0dc4624b5d850020 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 1 Apr 2025 11:46:12 -0400 Subject: [PATCH 16/45] object's components belong to the object --- .../Shared/Physics/GridReparentVelocity_Test.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index b3c53fde0c0..3d7d52a0c9d 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -65,7 +65,7 @@ await server.WaitPost(() => // Spawn our test object in the middle of the grid, ensure it has no damping. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(obj), 0.0f); + physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); }); await server.WaitAssertion(() => @@ -124,7 +124,7 @@ await server.WaitPost(() => // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(obj), 0.0f); + physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); }); await server.WaitAssertion(() => From 945f483fd96b81f236a0fae15b99000902414d76 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 20:09:02 -0400 Subject: [PATCH 17/45] Grid reparent angular velocity tests --- .../Physics/GridReparentVelocity_Test.cs | 135 +++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 3d7d52a0c9d..cf89effba55 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -29,7 +29,7 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest "; [Test] - public async Task TestLinearVelocityMoveOffGrid() + public async Task TestLinearVelocityOnlyMoveOffGrid() { var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; var server = StartServer(serverOpts); @@ -88,7 +88,7 @@ await server.WaitAssertion(() => } [Test] - public async Task TestLinearVelocityMoveOntoGrid() + public async Task TestLinearVelocityOnlyMoveOntoGrid() { var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; var server = StartServer(serverOpts); @@ -145,4 +145,135 @@ await server.WaitAssertion(() => Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); }); } + + [Test] + public async Task TestLinearAndAngularVelocityMoveOffGrid() + { + var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; + var server = StartServer(serverOpts); + + await server.WaitIdleAsync(); + + // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body + var systems = server.ResolveDependency(); + var fixtureSystem = systems.GetEntitySystem(); + var mapManager = server.ResolveDependency(); + var entManager = server.ResolveDependency(); + var physSystem = systems.GetEntitySystem(); + var transformSystem = entManager.EntitySysManager.GetEntitySystem(); + var mapSystem = entManager.EntitySysManager.GetEntitySystem(); + + // Set up entities + EntityUid map = default; + EntityUid grid = default; + EntityUid obj = default; + await server.WaitPost(() => + { + map = server.System().CreateMap(out var mapId); + + // Spawn a grid with one tile, ensure it's movable and its velocities have no damping. + var gridEnt = mapManager.CreateGridEntity(mapId); + physSystem.SetCanCollide(gridEnt, true); + physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); + physSystem.SetAngularDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); + + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + grid = gridEnt.Owner; + + // Spawn our test object in the middle of the grid, ensure it has no damping. + obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); + physSystem.SetCanCollide(obj, true); + physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); + physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); + }); + + await server.WaitAssertion(() => + { + // Our object should start on the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + + // Wait a second to clear the grid + physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + objPhys = entManager.GetComponent(obj); + Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(objPhys.AngularVelocity, Is.EqualTo(3.0f)); + gridPhys = entManager.GetComponent(obj); + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + }); + } + + [Test] + public async Task TestLinearAndAngularVelocityMoveOntoGrid() + { + var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; + var server = StartServer(serverOpts); + + await server.WaitIdleAsync(); + + // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body + var systems = server.ResolveDependency(); + var fixtureSystem = systems.GetEntitySystem(); + var mapManager = server.ResolveDependency(); + var entManager = server.ResolveDependency(); + var physSystem = systems.GetEntitySystem(); + var transformSystem = entManager.EntitySysManager.GetEntitySystem(); + var mapSystem = entManager.EntitySysManager.GetEntitySystem(); + + // Set up entities + EntityUid map = default; + EntityUid grid = default; + EntityUid obj = default; + await server.WaitPost(() => + { + map = server.System().CreateMap(out var mapId); + + // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. + var gridEnt = mapManager.CreateGridEntity(mapId); + physSystem.SetCanCollide(gridEnt, true); + physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); + physSystem.SetAngularDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); + + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + grid = gridEnt.Owner; + + // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. + obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); + physSystem.SetCanCollide(obj, true); + physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); + physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); + }); + + await server.WaitAssertion(() => + { + // Assert that we start off the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + + // Wait a second to move onto the middle of the grid + physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + // Not checking linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. + Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); + Assert.That(entManager.GetComponent(grid).AngularVelocity, Is.EqualTo(2.0f)); + }); + } } From fdc7c9c8145620744b7fb4d93cf1b1c8ca704a97 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 20:10:23 -0400 Subject: [PATCH 18/45] add angular velocity tests --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index cf89effba55..4b856b4040e 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -271,8 +271,9 @@ await server.WaitAssertion(() => // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - // Not checking linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. + // Not checking object's linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); Assert.That(entManager.GetComponent(grid).AngularVelocity, Is.EqualTo(2.0f)); }); } From 19ccfa639de45b8e049a1f04cafe005819fbac36 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 20:14:03 -0400 Subject: [PATCH 19/45] Fix angular velocity tests --- .../Shared/Physics/GridReparentVelocity_Test.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 4b856b4040e..b05cea54757 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -204,10 +204,10 @@ await server.WaitAssertion(() => // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - objPhys = entManager.GetComponent(obj); + var objPhys = entManager.GetComponent(obj); Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); Assert.That(objPhys.AngularVelocity, Is.EqualTo(3.0f)); - gridPhys = entManager.GetComponent(obj); + var gridPhys = entManager.GetComponent(obj); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); }); @@ -273,8 +273,9 @@ await server.WaitAssertion(() => Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Not checking object's linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); - Assert.That(entManager.GetComponent(grid).AngularVelocity, Is.EqualTo(2.0f)); + var gridPhys = entManager.GetComponent(grid).LinearVelocity; + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); }); } } From 4bbc842bf247bb3c47af60315f609e3b509bf986 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 20:17:14 -0400 Subject: [PATCH 20/45] fix grid/object ref --- .../Shared/Physics/GridReparentVelocity_Test.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index b05cea54757..31dac9fc004 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -207,7 +207,7 @@ await server.WaitAssertion(() => var objPhys = entManager.GetComponent(obj); Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); Assert.That(objPhys.AngularVelocity, Is.EqualTo(3.0f)); - var gridPhys = entManager.GetComponent(obj); + var gridPhys = entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); }); @@ -273,7 +273,7 @@ await server.WaitAssertion(() => Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Not checking object's linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); - var gridPhys = entManager.GetComponent(grid).LinearVelocity; + var gridPhys = entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); }); From 8ac79a09eefa236ed84a4cf4f1888f3f6e79deb9 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 20:22:03 -0400 Subject: [PATCH 21/45] Remove object velocity test from rotating grid --- .../Shared/Physics/GridReparentVelocity_Test.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 31dac9fc004..8d2039c3e23 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -204,9 +204,8 @@ await server.WaitAssertion(() => // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - var objPhys = entManager.GetComponent(obj); - Assert.That(objPhys.LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); - Assert.That(objPhys.AngularVelocity, Is.EqualTo(3.0f)); + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. + Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); var gridPhys = entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); @@ -271,7 +270,7 @@ await server.WaitAssertion(() => // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - // Not checking object's linear velocity in this case, non-zero angular velocity contribution from moving onto the grid. + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); var gridPhys = entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); From 12c556ab9c94042cd840937be681951fc25af8ed Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 21:13:19 -0400 Subject: [PATCH 22/45] reparent velocity: reference map system --- .../Shared/Physics/GridReparentVelocity_Test.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 8d2039c3e23..10128d24b1c 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -28,6 +28,7 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest hard: false "; + // Moves an object off of a moving grid, checks for conservation of linear velocity. [Test] public async Task TestLinearVelocityOnlyMoveOffGrid() { @@ -36,7 +37,6 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() await server.WaitIdleAsync(); - // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body var systems = server.ResolveDependency(); var fixtureSystem = systems.GetEntitySystem(); var mapManager = server.ResolveDependency(); @@ -51,7 +51,7 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() EntityUid obj = default; await server.WaitPost(() => { - map = server.System().CreateMap(out var mapId); + map = mapSystem.CreateMap(out var mapId); // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. var gridEnt = mapManager.CreateGridEntity(mapId); @@ -87,6 +87,7 @@ await server.WaitAssertion(() => }); } + // Moves an object onto a moving grid, checks for conservation of linear velocity. [Test] public async Task TestLinearVelocityOnlyMoveOntoGrid() { @@ -95,7 +96,6 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() await server.WaitIdleAsync(); - // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body var systems = server.ResolveDependency(); var fixtureSystem = systems.GetEntitySystem(); var mapManager = server.ResolveDependency(); @@ -110,7 +110,7 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() EntityUid obj = default; await server.WaitPost(() => { - map = server.System().CreateMap(out var mapId); + map = mapSystem.CreateMap(out var mapId); // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. var gridEnt = mapManager.CreateGridEntity(mapId); @@ -146,6 +146,7 @@ await server.WaitAssertion(() => }); } + // Moves a rotating object off of a rotating grid, checks for conservation of angular velocity. [Test] public async Task TestLinearAndAngularVelocityMoveOffGrid() { @@ -154,7 +155,6 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() await server.WaitIdleAsync(); - // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body var systems = server.ResolveDependency(); var fixtureSystem = systems.GetEntitySystem(); var mapManager = server.ResolveDependency(); @@ -169,7 +169,7 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() EntityUid obj = default; await server.WaitPost(() => { - map = server.System().CreateMap(out var mapId); + map = mapSystem.CreateMap(out var mapId); // Spawn a grid with one tile, ensure it's movable and its velocities have no damping. var gridEnt = mapManager.CreateGridEntity(mapId); @@ -212,6 +212,7 @@ await server.WaitAssertion(() => }); } + // Moves a rotating object onto a rotating grid, checks for conservation of angular velocity. [Test] public async Task TestLinearAndAngularVelocityMoveOntoGrid() { @@ -220,7 +221,6 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() await server.WaitIdleAsync(); - // Checks that FindGridContacts succesfully overlaps a grid + map broadphase physics body var systems = server.ResolveDependency(); var fixtureSystem = systems.GetEntitySystem(); var mapManager = server.ResolveDependency(); @@ -235,7 +235,7 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() EntityUid obj = default; await server.WaitPost(() => { - map = server.System().CreateMap(out var mapId); + map = mapSystem.CreateMap(out var mapId); // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. var gridEnt = mapManager.CreateGridEntity(mapId); From 2a6d040a5159833b74780bbefbcbb07be8fe22a4 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:24:03 -0400 Subject: [PATCH 23/45] velocity unit tests --- .../Physics/GridReparentVelocity_Test.cs | 340 +++++++----------- 1 file changed, 131 insertions(+), 209 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 10128d24b1c..a9a71e98825 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -1,12 +1,14 @@ using System.Numerics; using System.Threading.Tasks; using NUnit.Framework; +using Robust.Client.Physics; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Physics; using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Systems; +using Robust.UnitTesting.Server; namespace Robust.UnitTesting.Shared.Physics; @@ -32,249 +34,169 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest [Test] public async Task TestLinearVelocityOnlyMoveOffGrid() { - var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; - var server = StartServer(serverOpts); + var sim = RobustServerSimulation.NewSimulation(); + var server = sim.InitializeInstance(); - await server.WaitIdleAsync(); - - var systems = server.ResolveDependency(); - var fixtureSystem = systems.GetEntitySystem(); - var mapManager = server.ResolveDependency(); - var entManager = server.ResolveDependency(); + var systems = server.Resolve(); + var entManager = server.Resolve(); var physSystem = systems.GetEntitySystem(); - var transformSystem = entManager.EntitySysManager.GetEntitySystem(); var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - // Set up entities - EntityUid map = default; - EntityUid grid = default; - EntityUid obj = default; - await server.WaitPost(() => - { - map = mapSystem.CreateMap(out var mapId); - - // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. - var gridEnt = mapManager.CreateGridEntity(mapId); - physSystem.SetCanCollide(gridEnt, true); - physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); - - mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); - grid = gridEnt.Owner; - - // Spawn our test object in the middle of the grid, ensure it has no damping. - obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); - physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); - }); - - await server.WaitAssertion(() => - { - // Our object should start on the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - - // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); - - // Wait a second to clear the grid - physSystem.Update(1.0f); - - // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); - }); + // Spawn our test object in the middle of the grid, ensure it has no damping. + EntityUid map = mapSystem.CreateMap(out var mapId); + EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); + EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f), physSystem, entManager); + + // Our object should start on the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + + // Wait a second to clear the grid + physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); } // Moves an object onto a moving grid, checks for conservation of linear velocity. [Test] public async Task TestLinearVelocityOnlyMoveOntoGrid() { - var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; - var server = StartServer(serverOpts); - - await server.WaitIdleAsync(); + var sim = RobustServerSimulation.NewSimulation(); + var server = sim.InitializeInstance(); - var systems = server.ResolveDependency(); - var fixtureSystem = systems.GetEntitySystem(); - var mapManager = server.ResolveDependency(); - var entManager = server.ResolveDependency(); + var systems = server.Resolve(); + var entManager = server.Resolve(); var physSystem = systems.GetEntitySystem(); - var transformSystem = entManager.EntitySysManager.GetEntitySystem(); var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - // Set up entities - EntityUid map = default; - EntityUid grid = default; - EntityUid obj = default; - await server.WaitPost(() => - { - map = mapSystem.CreateMap(out var mapId); - - // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. - var gridEnt = mapManager.CreateGridEntity(mapId); - physSystem.SetCanCollide(gridEnt, true); - physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); - - mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); - grid = gridEnt.Owner; - - // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. - obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); - physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); - }); - - await server.WaitAssertion(() => - { - // Assert that we start off the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - - // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); - - // Wait a second to move onto the middle of the grid - physSystem.Update(1.0f); - - // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); - }); + // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. + EntityUid map = mapSystem.CreateMap(out var mapId); + EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); + EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f), physSystem, entManager); + + // Assert that we start off the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + + // Wait a second to move onto the middle of the grid + physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); } // Moves a rotating object off of a rotating grid, checks for conservation of angular velocity. [Test] public async Task TestLinearAndAngularVelocityMoveOffGrid() { - var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; - var server = StartServer(serverOpts); + var sim = RobustServerSimulation.NewSimulation(); + var server = sim.InitializeInstance(); - await server.WaitIdleAsync(); - - var systems = server.ResolveDependency(); - var fixtureSystem = systems.GetEntitySystem(); - var mapManager = server.ResolveDependency(); - var entManager = server.ResolveDependency(); + var systems = server.Resolve(); + var entManager = server.Resolve(); var physSystem = systems.GetEntitySystem(); - var transformSystem = entManager.EntitySysManager.GetEntitySystem(); - var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - - // Set up entities - EntityUid map = default; - EntityUid grid = default; - EntityUid obj = default; - await server.WaitPost(() => - { - map = mapSystem.CreateMap(out var mapId); - - // Spawn a grid with one tile, ensure it's movable and its velocities have no damping. - var gridEnt = mapManager.CreateGridEntity(mapId); - physSystem.SetCanCollide(gridEnt, true); - physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); - physSystem.SetAngularDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); - - mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); - grid = gridEnt.Owner; - - // Spawn our test object in the middle of the grid, ensure it has no damping. - obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(grid, 0.5f, 0.5f)); - physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); - physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); - }); - - await server.WaitAssertion(() => - { - // Our object should start on the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - - // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); - - // Wait a second to clear the grid - physSystem.Update(1.0f); - - // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); - var gridPhys = entManager.GetComponent(grid); - Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); - Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); - }); + var mapSystem = systems.GetEntitySystem(); + + // Spawn our test object in the middle of the grid, ensure it has no damping. + EntityUid map = mapSystem.CreateMap(out var mapId); + EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); + EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f), physSystem, entManager); + + // Our object should start on the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + + // Wait a second to clear the grid + physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. + Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); + var gridPhys = entManager.GetComponent(grid); + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } // Moves a rotating object onto a rotating grid, checks for conservation of angular velocity. [Test] public async Task TestLinearAndAngularVelocityMoveOntoGrid() { - var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes }; - var server = StartServer(serverOpts); - - await server.WaitIdleAsync(); + var sim = RobustServerSimulation.NewSimulation(); + var server = sim.InitializeInstance(); - var systems = server.ResolveDependency(); - var fixtureSystem = systems.GetEntitySystem(); - var mapManager = server.ResolveDependency(); - var entManager = server.ResolveDependency(); + var systems = server.Resolve(); + var entManager = server.Resolve(); var physSystem = systems.GetEntitySystem(); - var transformSystem = entManager.EntitySysManager.GetEntitySystem(); - var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - - // Set up entities - EntityUid map = default; - EntityUid grid = default; - EntityUid obj = default; - await server.WaitPost(() => - { - map = mapSystem.CreateMap(out var mapId); - - // Spawn a grid with one tile, ensure it's movable and its velocity has no damping. - var gridEnt = mapManager.CreateGridEntity(mapId); - physSystem.SetCanCollide(gridEnt, true); - physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - physSystem.SetLinearDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); - physSystem.SetAngularDamping(gridEnt, entManager.GetComponent(gridEnt), 0.0f); + var mapSystem = systems.GetEntitySystem(); + + // Spawn our test object in the middle of the grid, ensure it has no damping. + EntityUid map = mapSystem.CreateMap(out var mapId); + EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); + EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f), physSystem, entManager); + + // Assert that we start off the grid. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + + // Set the velocity of the grid and our object. + Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + + // Wait a second to move onto the middle of the grid + physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. + Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); + var gridPhys = entManager.GetComponent(grid); + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + } - mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); - grid = gridEnt.Owner; + // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. + public EntityUid SetupTestGrid(MapId map, ISimulation server, IEntitySystemManager systems, IEntityManager entManager, SharedMapSystem mapSystem) + { + var physSystem = systems.GetEntitySystem(); + var mapManager = server.Resolve(); - // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. - obj = server.EntMan.SpawnEntity("ReparentTestObject", new EntityCoordinates(map, 1.5f, 1.5f)); - physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); - physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); - }); + var gridEnt = mapManager.CreateGridEntity(map); + physSystem.SetCanCollide(gridEnt, true); + physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + var gridPhys = entManager.GetComponent(gridEnt); + physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); + physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); - await server.WaitAssertion(() => - { - // Assert that we start off the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - - // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + return gridEnt.Owner; + } - // Wait a second to move onto the middle of the grid - physSystem.Update(1.0f); + // Spawn a test object at the given position, ensure its velocity has no damping. + public EntityUid SetupTestObject(EntityCoordinates coords, SharedPhysicsSystem physSystem, IEntityManager entManager) + { + var obj = entManager.SpawnEntity("ReparentTestObject", coords); + physSystem.SetCanCollide(obj, true); + physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); + physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); - // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); - var gridPhys = entManager.GetComponent(grid); - Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); - Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); - }); + return obj; } } From 32260dba560aa92104610b8c23f3db6cbd2604bd Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:24:43 -0400 Subject: [PATCH 24/45] unused import --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index a9a71e98825..6f4d1e98591 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -1,7 +1,6 @@ using System.Numerics; using System.Threading.Tasks; using NUnit.Framework; -using Robust.Client.Physics; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; From ff0c5641d7ee18a1ca3be381eae5d7c7289da372 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:38:36 -0400 Subject: [PATCH 25/45] Cleanup --- .../Physics/GridReparentVelocity_Test.cs | 181 +++++++++--------- 1 file changed, 91 insertions(+), 90 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 6f4d1e98591..f12cc9d4f66 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -29,172 +29,173 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest hard: false "; + private ISimulation _sim = default!; + private IEntitySystemManager _systems = default!; + private IEntityManager _entManager = default!; + private IMapManager _mapManager = default!; + private SharedPhysicsSystem _physSystem = default!; + private SharedMapSystem _mapSystem = default!; + + // Test objects. + private EntityUid _mapUid = default!; + private EntityUid _gridUid = default!; + private EntityUid _objUid = default!; + + [OneTimeSetUp] + public void FixtureSetup() + { + _sim = RobustServerSimulation.NewSimulation() + .RegisterPrototypes(protoMan => protoMan.LoadString(Prototypes)) + .InitializeInstance(); + + _systems = _sim.Resolve(); + _entManager = _sim.Resolve(); + _mapManager = _sim.Resolve(); + _physSystem = _systems.GetEntitySystem(); + _mapSystem = _systems.GetEntitySystem(); + } + + [SetUp] + public void Setup() + { + _mapUid = _mapSystem.CreateMap(out var mapId); + _gridUid = SetupTestGrid(mapId); + } + + [TearDown] + public void Teardown() + { + _entManager.DeleteEntity(_gridUid); + _gridUid = default!; + _entManager.DeleteEntity(_objUid); + _objUid = default!; + } + // Moves an object off of a moving grid, checks for conservation of linear velocity. [Test] public async Task TestLinearVelocityOnlyMoveOffGrid() { - var sim = RobustServerSimulation.NewSimulation(); - var server = sim.InitializeInstance(); - - var systems = server.Resolve(); - var entManager = server.Resolve(); - var physSystem = systems.GetEntitySystem(); - var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid map = mapSystem.CreateMap(out var mapId); - EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); - EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f), physSystem, entManager); + EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f)); // Our object should start on the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); // Wait a second to clear the grid - physSystem.Update(1.0f); + _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(_entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); } - // Moves an object onto a moving grid, checks for conservation of linear velocity. [Test] + // Moves an object onto a moving grid, checks for conservation of linear velocity. public async Task TestLinearVelocityOnlyMoveOntoGrid() { - var sim = RobustServerSimulation.NewSimulation(); - var server = sim.InitializeInstance(); - - var systems = server.Resolve(); - var entManager = server.Resolve(); - var physSystem = systems.GetEntitySystem(); - var mapSystem = entManager.EntitySysManager.GetEntitySystem(); - // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. - EntityUid map = mapSystem.CreateMap(out var mapId); - EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); - EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f), physSystem, entManager); + EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f)); // Assert that we start off the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); // Wait a second to move onto the middle of the grid - physSystem.Update(1.0f); + _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - Assert.That(entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); - Assert.That(entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(_entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); } - // Moves a rotating object off of a rotating grid, checks for conservation of angular velocity. [Test] + // Moves a rotating object off of a rotating grid, checks for conservation of angular velocity. public async Task TestLinearAndAngularVelocityMoveOffGrid() { - var sim = RobustServerSimulation.NewSimulation(); - var server = sim.InitializeInstance(); - - var systems = server.Resolve(); - var entManager = server.Resolve(); - var physSystem = systems.GetEntitySystem(); - var mapSystem = systems.GetEntitySystem(); - // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid map = mapSystem.CreateMap(out var mapId); - EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); - EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f), physSystem, entManager); + EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f)); // Our object should start on the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(grid, 2.0f), Is.True); // Wait a second to clear the grid - physSystem.Update(1.0f); + _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); - var gridPhys = entManager.GetComponent(grid); + Assert.That(_entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); + var gridPhys = _entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } - // Moves a rotating object onto a rotating grid, checks for conservation of angular velocity. [Test] + // Moves a rotating object onto a rotating grid, checks for conservation of angular velocity. public async Task TestLinearAndAngularVelocityMoveOntoGrid() { - var sim = RobustServerSimulation.NewSimulation(); - var server = sim.InitializeInstance(); - - var systems = server.Resolve(); - var entManager = server.Resolve(); - var physSystem = systems.GetEntitySystem(); - var mapSystem = systems.GetEntitySystem(); - // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid map = mapSystem.CreateMap(out var mapId); - EntityUid grid = SetupTestGrid(mapId, server, systems, entManager, mapSystem); - EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f), physSystem, entManager); + EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f)); // Assert that we start off the grid. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); // Set the velocity of the grid and our object. - Assert.That(physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); - Assert.That(physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(obj, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(grid, 2.0f), Is.True); // Wait a second to move onto the middle of the grid - physSystem.Update(1.0f); + _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); - var gridPhys = entManager.GetComponent(grid); + Assert.That(_entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); + var gridPhys = _entManager.GetComponent(grid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. - public EntityUid SetupTestGrid(MapId map, ISimulation server, IEntitySystemManager systems, IEntityManager entManager, SharedMapSystem mapSystem) + public EntityUid SetupTestGrid(MapId map) { - var physSystem = systems.GetEntitySystem(); - var mapManager = server.Resolve(); + var physSystem = _systems.GetEntitySystem(); - var gridEnt = mapManager.CreateGridEntity(map); + var gridEnt = _mapManager.CreateGridEntity(map); physSystem.SetCanCollide(gridEnt, true); physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - var gridPhys = entManager.GetComponent(gridEnt); + var gridPhys = _entManager.GetComponent(gridEnt); physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); - mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + _mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); return gridEnt.Owner; } // Spawn a test object at the given position, ensure its velocity has no damping. - public EntityUid SetupTestObject(EntityCoordinates coords, SharedPhysicsSystem physSystem, IEntityManager entManager) + public EntityUid SetupTestObject(EntityCoordinates coords) { - var obj = entManager.SpawnEntity("ReparentTestObject", coords); - physSystem.SetCanCollide(obj, true); - physSystem.SetLinearDamping(obj, entManager.GetComponent(obj), 0.0f); - physSystem.SetAngularDamping(obj, entManager.GetComponent(obj), 0.0f); + var obj = _entManager.SpawnEntity("ReparentTestObject", coords); + _physSystem.SetCanCollide(obj, true); + _physSystem.SetLinearDamping(obj, _entManager.GetComponent(obj), 0.0f); + _physSystem.SetAngularDamping(obj, _entManager.GetComponent(obj), 0.0f); return obj; } From 32356c1c77ffe406efb2b40e8b7451353263e9ac Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:42:02 -0400 Subject: [PATCH 26/45] fix UID references --- .../Physics/GridReparentVelocity_Test.cs | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index f12cc9d4f66..38dc9405635 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -76,22 +76,22 @@ public void Teardown() public async Task TestLinearVelocityOnlyMoveOffGrid() { // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_gridUid, 0.5f, 0.5f)); // Our object should start on the grid. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); // Wait a second to clear the grid _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); - Assert.That(_entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); - Assert.That(_entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); } [Test] @@ -99,22 +99,22 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() public async Task TestLinearVelocityOnlyMoveOntoGrid() { // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. - EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); // Assert that we start off the grid. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); // Wait a second to move onto the middle of the grid _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); - Assert.That(_entManager.GetComponent(obj).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); - Assert.That(_entManager.GetComponent(grid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); } [Test] @@ -122,25 +122,25 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() public async Task TestLinearAndAngularVelocityMoveOffGrid() { // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid obj = SetupTestObject(new EntityCoordinates(grid, 0.5f, 0.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_gridUid, 0.5f, 0.5f)); // Our object should start on the grid. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(1.0f, 2.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); // Wait a second to clear the grid _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(_entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(3.0f)); - var gridPhys = _entManager.GetComponent(grid); + Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(3.0f)); + var gridPhys = _entManager.GetComponent(_gridUid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } @@ -150,25 +150,25 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() public async Task TestLinearAndAngularVelocityMoveOntoGrid() { // Spawn our test object in the middle of the grid, ensure it has no damping. - EntityUid obj = SetupTestObject(new EntityCoordinates(map, 1.5f, 1.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); // Assert that we start off the grid. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(map)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(obj, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(obj, 1.0f), Is.True); - Assert.That(_physSystem.SetLinearVelocity(grid, new Vector2(-1.0f, -2.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(grid, 2.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); // Wait a second to move onto the middle of the grid _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(obj).ParentUid, Is.EqualTo(grid)); + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(_entManager.GetComponent(obj).AngularVelocity, Is.EqualTo(-1.0f)); - var gridPhys = _entManager.GetComponent(grid); + Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(-1.0f)); + var gridPhys = _entManager.GetComponent(_gridUid); Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } From 36612fc6814ec1c3d4cafdfd674ea18a5e7151f1 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:43:18 -0400 Subject: [PATCH 27/45] create one map in one-time setup --- .../Shared/Physics/GridReparentVelocity_Test.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 38dc9405635..f520524ab86 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -38,6 +38,7 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest // Test objects. private EntityUid _mapUid = default!; + private MapId _mapId = default!; private EntityUid _gridUid = default!; private EntityUid _objUid = default!; @@ -53,13 +54,14 @@ public void FixtureSetup() _mapManager = _sim.Resolve(); _physSystem = _systems.GetEntitySystem(); _mapSystem = _systems.GetEntitySystem(); + + _mapUid = _mapSystem.CreateMap(out _mapId); } [SetUp] public void Setup() { - _mapUid = _mapSystem.CreateMap(out var mapId); - _gridUid = SetupTestGrid(mapId); + _gridUid = SetupTestGrid(_mapId); } [TearDown] From 57d152c033ef705dc344d9f9c1bb5ec6aa57731c Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:44:35 -0400 Subject: [PATCH 28/45] Fix comments --- .../Shared/Physics/GridReparentVelocity_Test.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index f520524ab86..702d3ded306 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -100,7 +100,7 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() // Moves an object onto a moving grid, checks for conservation of linear velocity. public async Task TestLinearVelocityOnlyMoveOntoGrid() { - // Spawn our test object 1 m off of the middle of the grid in both directions, ensure it has no damping. + // Spawn our test object 1 m off of the middle of the grid in both directions. _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); // Assert that we start off the grid. @@ -123,7 +123,7 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() // Moves a rotating object off of a rotating grid, checks for conservation of angular velocity. public async Task TestLinearAndAngularVelocityMoveOffGrid() { - // Spawn our test object in the middle of the grid, ensure it has no damping. + // Spawn our test object in the middle of the grid. _objUid = SetupTestObject(new EntityCoordinates(_gridUid, 0.5f, 0.5f)); // Our object should start on the grid. @@ -151,7 +151,7 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() // Moves a rotating object onto a rotating grid, checks for conservation of angular velocity. public async Task TestLinearAndAngularVelocityMoveOntoGrid() { - // Spawn our test object in the middle of the grid, ensure it has no damping. + // Spawn our test object 1 m off of the middle of the grid in both directions. _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); // Assert that we start off the grid. From 6292b369357e87602f3facf85114137d591dce51 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:53:35 -0400 Subject: [PATCH 29/45] Create fixture in SetupTestObject function --- .../Physics/GridReparentVelocity_Test.cs | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 702d3ded306..0bf68828d66 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; using NUnit.Framework; @@ -5,7 +6,9 @@ using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Physics; +using Robust.Shared.Physics.Collision.Shapes; using Robust.Shared.Physics.Components; +using Robust.Shared.Physics.Dynamics; using Robust.Shared.Physics.Systems; using Robust.UnitTesting.Server; @@ -20,21 +23,15 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest components: - type: Physics bodyType: Dynamic - - type: Fixtures - fixtures: - fix1: - shape: - !type:PhysShapeAabb - bounds: '-0.1,-0.1,0.1,0.1' - hard: false "; private ISimulation _sim = default!; private IEntitySystemManager _systems = default!; private IEntityManager _entManager = default!; private IMapManager _mapManager = default!; - private SharedPhysicsSystem _physSystem = default!; + private FixtureSystem _fixtureSystem = default!; private SharedMapSystem _mapSystem = default!; + private SharedPhysicsSystem _physSystem = default!; // Test objects. private EntityUid _mapUid = default!; @@ -52,8 +49,9 @@ public void FixtureSetup() _systems = _sim.Resolve(); _entManager = _sim.Resolve(); _mapManager = _sim.Resolve(); - _physSystem = _systems.GetEntitySystem(); + _fixtureSystem = _systems.GetEntitySystem(); _mapSystem = _systems.GetEntitySystem(); + _physSystem = _systems.GetEntitySystem(); _mapUid = _mapSystem.CreateMap(out _mapId); } @@ -191,10 +189,22 @@ public EntityUid SetupTestGrid(MapId map) return gridEnt.Owner; } - // Spawn a test object at the given position, ensure its velocity has no damping. + // Spawn a bullet-like test object at the given position, ensure its velocity has no damping. public EntityUid SetupTestObject(EntityCoordinates coords) { var obj = _entManager.SpawnEntity("ReparentTestObject", coords); + + _entManager.EnsureComponent(obj); + var poly = new PolygonShape(); + poly.Set(new List() + { + new(0.1f, -0.1f), + new(0.1f, 0.1f), + new(-0.1f, 0.1f), + new(-0.1f, -0.1f), + }); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 1, 1, false)); + _physSystem.SetCanCollide(obj, true); _physSystem.SetLinearDamping(obj, _entManager.GetComponent(obj), 0.0f); _physSystem.SetAngularDamping(obj, _entManager.GetComponent(obj), 0.0f); From a4c91c413b88c4c2adbd89fb9df1a664dac2307b Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 22:56:23 -0400 Subject: [PATCH 30/45] Move grid setup to Setup function --- .../Physics/GridReparentVelocity_Test.cs | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 0bf68828d66..08f2388eca0 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -59,7 +59,16 @@ public void FixtureSetup() [SetUp] public void Setup() { - _gridUid = SetupTestGrid(_mapId); + // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. + var gridEnt = _mapManager.CreateGridEntity(_mapId); + _physSystem.SetCanCollide(gridEnt, true); + _physSystem.SetBodyType(gridEnt, BodyType.Dynamic); + var gridPhys = _entManager.GetComponent(gridEnt); + _physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); + _physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); + + _mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + _gridUid = gridEnt.Owner; } [TearDown] @@ -173,22 +182,6 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); } - // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. - public EntityUid SetupTestGrid(MapId map) - { - var physSystem = _systems.GetEntitySystem(); - - var gridEnt = _mapManager.CreateGridEntity(map); - physSystem.SetCanCollide(gridEnt, true); - physSystem.SetBodyType(gridEnt, BodyType.Dynamic); - var gridPhys = _entManager.GetComponent(gridEnt); - physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); - physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); - - _mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); - return gridEnt.Owner; - } - // Spawn a bullet-like test object at the given position, ensure its velocity has no damping. public EntityUid SetupTestObject(EntityCoordinates coords) { From d25f9c7bd4e37459b66f1cb577dddabca4651e0e Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:01:44 -0400 Subject: [PATCH 31/45] AssertMultiple --- .../Physics/GridReparentVelocity_Test.cs | 146 ++++++++++-------- 1 file changed, 79 insertions(+), 67 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 08f2388eca0..18633829fbe 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -87,20 +87,23 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() // Spawn our test object in the middle of the grid, ensure it has no damping. _objUid = SetupTestObject(new EntityCoordinates(_gridUid, 0.5f, 0.5f)); - // Our object should start on the grid. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - - // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); - - // Wait a second to clear the grid - _physSystem.Update(1.0f); - - // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); - Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.Multiple(() => + { + // Our object should start on the grid. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + + // Set the velocity of the grid and our object. + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); + + // Wait a second to clear the grid + _physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); + Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + }); } [Test] @@ -110,20 +113,23 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() // Spawn our test object 1 m off of the middle of the grid in both directions. _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); - // Assert that we start off the grid. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - - // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); - - // Wait a second to move onto the middle of the grid - _physSystem.Update(1.0f); - - // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); - Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.Multiple(() => + { + // Assert that we start off the grid. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + + // Set the velocity of the grid and our object. + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); + + // Wait a second to move onto the middle of the grid + _physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); + Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + }); } [Test] @@ -133,25 +139,28 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() // Spawn our test object in the middle of the grid. _objUid = SetupTestObject(new EntityCoordinates(_gridUid, 0.5f, 0.5f)); - // Our object should start on the grid. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - - // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); - Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); - - // Wait a second to clear the grid - _physSystem.Update(1.0f); - - // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(3.0f)); - var gridPhys = _entManager.GetComponent(_gridUid); - Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); - Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + Assert.Multiple(() => + { + // Our object should start on the grid. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + + // Set the velocity of the grid and our object. + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); + + // Wait a second to clear the grid + _physSystem.Update(1.0f); + + // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. + Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(3.0f)); + var gridPhys = _entManager.GetComponent(_gridUid); + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + }); } [Test] @@ -161,25 +170,28 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() // Spawn our test object 1 m off of the middle of the grid in both directions. _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); - // Assert that we start off the grid. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - - // Set the velocity of the grid and our object. - Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); - Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); - Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); - - // Wait a second to move onto the middle of the grid - _physSystem.Update(1.0f); - - // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. - Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(-1.0f)); - var gridPhys = _entManager.GetComponent(_gridUid); - Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); - Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + Assert.Multiple(() => + { + // Assert that we start off the grid. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + + // Set the velocity of the grid and our object. + Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); + Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); + Assert.That(_physSystem.SetAngularVelocity(_gridUid, 2.0f), Is.True); + + // Wait a second to move onto the middle of the grid + _physSystem.Update(1.0f); + + // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. + Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. + Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(-1.0f)); + var gridPhys = _entManager.GetComponent(_gridUid); + Assert.That(gridPhys.LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); + Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); + }); } // Spawn a bullet-like test object at the given position, ensure its velocity has no damping. @@ -196,7 +208,7 @@ public EntityUid SetupTestObject(EntityCoordinates coords) new(-0.1f, 0.1f), new(-0.1f, -0.1f), }); - _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 1, 1, false)); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false)); _physSystem.SetCanCollide(obj, true); _physSystem.SetLinearDamping(obj, _entManager.GetComponent(obj), 0.0f); From aa61fb538bb1a358211d1e8443acafe8b6566d05 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:05:59 -0400 Subject: [PATCH 32/45] More cleanup (no prototypes) --- .../Physics/GridReparentVelocity_Test.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 18633829fbe..0177ec84b85 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -17,14 +17,6 @@ namespace Robust.UnitTesting.Shared.Physics; [TestFixture, TestOf(typeof(SharedPhysicsSystem))] public sealed class GridReparentVelocity_Test : RobustIntegrationTest { - private static readonly string Prototypes = @" -- type: entity - id: ReparentTestObject - components: - - type: Physics - bodyType: Dynamic -"; - private ISimulation _sim = default!; private IEntitySystemManager _systems = default!; private IEntityManager _entManager = default!; @@ -43,7 +35,6 @@ public sealed class GridReparentVelocity_Test : RobustIntegrationTest public void FixtureSetup() { _sim = RobustServerSimulation.NewSimulation() - .RegisterPrototypes(protoMan => protoMan.LoadString(Prototypes)) .InitializeInstance(); _systems = _sim.Resolve(); @@ -61,9 +52,9 @@ public void Setup() { // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. var gridEnt = _mapManager.CreateGridEntity(_mapId); - _physSystem.SetCanCollide(gridEnt, true); - _physSystem.SetBodyType(gridEnt, BodyType.Dynamic); var gridPhys = _entManager.GetComponent(gridEnt); + _physSystem.SetBodyType(gridEnt, BodyType.Dynamic, body: gridPhys); + _physSystem.SetCanCollide(gridEnt, true, body: gridPhys); _physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); _physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); @@ -194,12 +185,15 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() }); } - // Spawn a bullet-like test object at the given position, ensure its velocity has no damping. + // Spawn a bullet-like test object at the given position. public EntityUid SetupTestObject(EntityCoordinates coords) { - var obj = _entManager.SpawnEntity("ReparentTestObject", coords); + var obj = _entManager.SpawnEntity(null, coords); + _entManager.EnsureComponent(obj); _entManager.EnsureComponent(obj); + + // Set up fixture. var poly = new PolygonShape(); poly.Set(new List() { @@ -210,6 +204,8 @@ public EntityUid SetupTestObject(EntityCoordinates coords) }); _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false)); + // Set up physics (no velocity damping, dynamic body, physics enabled) + _physSystem.SetBodyType(obj, BodyType.Dynamic); _physSystem.SetCanCollide(obj, true); _physSystem.SetLinearDamping(obj, _entManager.GetComponent(obj), 0.0f); _physSystem.SetAngularDamping(obj, _entManager.GetComponent(obj), 0.0f); From 27483b9ad737d7da8f81f322d19ad6b21fef8512 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:29:57 -0400 Subject: [PATCH 33/45] cleanup --- .../Physics/GridReparentVelocity_Test.cs | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 0177ec84b85..a71e8c4aa2e 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -54,7 +54,6 @@ public void Setup() var gridEnt = _mapManager.CreateGridEntity(_mapId); var gridPhys = _entManager.GetComponent(gridEnt); _physSystem.SetBodyType(gridEnt, BodyType.Dynamic, body: gridPhys); - _physSystem.SetCanCollide(gridEnt, true, body: gridPhys); _physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); _physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); @@ -62,6 +61,34 @@ public void Setup() _gridUid = gridEnt.Owner; } + // Spawn a bullet-like test object at the given position. + public EntityUid SetupTestObject(EntityCoordinates coords) + { + var obj = _entManager.SpawnEntity(null, coords); + + _entManager.EnsureComponent(obj); + _entManager.EnsureComponent(obj); + + // Set up fixture. + var poly = new PolygonShape(); + poly.Set(new List() + { + new(0.1f, -0.1f), + new(0.1f, 0.1f), + new(-0.1f, 0.1f), + new(-0.1f, -0.1f), + }); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false)); + + // Set up physics (no velocity damping, dynamic body, physics enabled) + var objPhys = _entManager.GetComponent(obj); + _physSystem.SetBodyType(obj, BodyType.Dynamic, body: objPhys); + _physSystem.SetLinearDamping(obj, objPhys, 0.0f); + _physSystem.SetAngularDamping(obj, objPhys, 0.0f); + + return obj; + } + [TearDown] public void Teardown() { @@ -184,32 +211,4 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() Assert.That(gridPhys.AngularVelocity, Is.EqualTo(2.0f)); }); } - - // Spawn a bullet-like test object at the given position. - public EntityUid SetupTestObject(EntityCoordinates coords) - { - var obj = _entManager.SpawnEntity(null, coords); - - _entManager.EnsureComponent(obj); - _entManager.EnsureComponent(obj); - - // Set up fixture. - var poly = new PolygonShape(); - poly.Set(new List() - { - new(0.1f, -0.1f), - new(0.1f, 0.1f), - new(-0.1f, 0.1f), - new(-0.1f, -0.1f), - }); - _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false)); - - // Set up physics (no velocity damping, dynamic body, physics enabled) - _physSystem.SetBodyType(obj, BodyType.Dynamic); - _physSystem.SetCanCollide(obj, true); - _physSystem.SetLinearDamping(obj, _entManager.GetComponent(obj), 0.0f); - _physSystem.SetAngularDamping(obj, _entManager.GetComponent(obj), 0.0f); - - return obj; - } } From 27f4a16da8f2d919466d2711ebd7b7084c104606 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:40:18 -0400 Subject: [PATCH 34/45] print out on assertion failures --- .../Shared/Physics/GridReparentVelocity_Test.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index a71e8c4aa2e..abd8779850d 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -118,7 +118,9 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + var objXform = _entManager.GetComponent(_objUid); + var gridXform = _entManager.GetComponent(_gridUid); + Assert.That(objXform.ParentUid, Is.EqualTo(_mapUid), $"Object is not on map - actual position: {objXform.ParentUid} {objXform.LocalPosition}, grid position: {gridXform.ParentUid} {gridXform.LocalPosition}"); Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(4.5f, 6.75f))); Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(1.0f, 2.0f))); }); @@ -144,7 +146,9 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + var objXform = _entManager.GetComponent(_objUid); + var gridXform = _entManager.GetComponent(_gridUid); + Assert.That(objXform.ParentUid, Is.EqualTo(_gridUid), $"Object is not on grid - actual position: {objXform.ParentUid} {objXform.LocalPosition}, grid position: {gridXform.ParentUid} {gridXform.LocalPosition}"); Assert.That(_entManager.GetComponent(_objUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -1.0f))); Assert.That(_entManager.GetComponent(_gridUid).LinearVelocity, Is.EqualTo(new Vector2(-1.0f, -2.0f))); }); @@ -172,7 +176,9 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() _physSystem.Update(1.0f); // The object should be parented to the map and maintain its map velocity, the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); + var objXform = _entManager.GetComponent(_objUid); + var gridXform = _entManager.GetComponent(_gridUid); + Assert.That(objXform.ParentUid, Is.EqualTo(_mapUid), $"Object is not on map - actual position: {objXform.ParentUid} {objXform.LocalPosition}, grid position: {gridXform.ParentUid} {gridXform.LocalPosition}"); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(3.0f)); var gridPhys = _entManager.GetComponent(_gridUid); @@ -203,7 +209,9 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() _physSystem.Update(1.0f); // The object should be parented to the grid and maintain its map velocity (slowing down), the grid should be unchanged. - Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); + var objXform = _entManager.GetComponent(_objUid); + var gridXform = _entManager.GetComponent(_gridUid); + Assert.That(objXform.ParentUid, Is.EqualTo(_gridUid), $"Object is not on grid - actual position: {objXform.ParentUid} {objXform.LocalPosition}, grid position: {gridXform.ParentUid} {gridXform.LocalPosition}"); // Not checking object's linear velocity in this case, non-zero contribution from grid angular velocity. Assert.That(_entManager.GetComponent(_objUid).AngularVelocity, Is.EqualTo(-1.0f)); var gridPhys = _entManager.GetComponent(_gridUid); From 41175775e500c655a16c7b14367bb2b39c3eaaaf Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:51:19 -0400 Subject: [PATCH 35/45] fixture after physics --- .../Physics/GridReparentVelocity_Test.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index abd8779850d..4adf1561a7e 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -66,8 +66,14 @@ public EntityUid SetupTestObject(EntityCoordinates coords) { var obj = _entManager.SpawnEntity(null, coords); - _entManager.EnsureComponent(obj); - _entManager.EnsureComponent(obj); + var objPhys = _entManager.EnsureComponent(obj); + var objFix = _entManager.EnsureComponent(obj); + + // Set up physics (no velocity damping, dynamic body, physics enabled) + _entManager.GetComponent(obj); + _physSystem.SetBodyType(obj, BodyType.Dynamic, body: objPhys); + _physSystem.SetLinearDamping(obj, objPhys, 0.0f); + _physSystem.SetAngularDamping(obj, objPhys, 0.0f); // Set up fixture. var poly = new PolygonShape(); @@ -78,13 +84,8 @@ public EntityUid SetupTestObject(EntityCoordinates coords) new(-0.1f, 0.1f), new(-0.1f, -0.1f), }); - _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false)); - - // Set up physics (no velocity damping, dynamic body, physics enabled) - var objPhys = _entManager.GetComponent(obj); - _physSystem.SetBodyType(obj, BodyType.Dynamic, body: objPhys); - _physSystem.SetLinearDamping(obj, objPhys, 0.0f); - _physSystem.SetAngularDamping(obj, objPhys, 0.0f); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false), manager: objFix, body: objPhys); + _physSystem.WakeBody(obj, manager: objFix, body: objPhys); return obj; } From efced5f6ab5deca3c51105c96d1ad95737096cd2 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Thu, 3 Apr 2025 23:57:38 -0400 Subject: [PATCH 36/45] move the object --- .../Shared/Physics/GridReparentVelocity_Test.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 4adf1561a7e..f4f7d101e62 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -132,7 +132,7 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() public async Task TestLinearVelocityOnlyMoveOntoGrid() { // Spawn our test object 1 m off of the middle of the grid in both directions. - _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.0f, 1.0f)); Assert.Multiple(() => { @@ -193,7 +193,7 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() public async Task TestLinearAndAngularVelocityMoveOntoGrid() { // Spawn our test object 1 m off of the middle of the grid in both directions. - _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.0f, 1.0f)); Assert.Multiple(() => { From c98f62fc1d96310d43a69b6c534a794f985d34c6 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:08:38 -0400 Subject: [PATCH 37/45] restore the CanCollide after setting up fixtures --- .../Shared/Physics/GridReparentVelocity_Test.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index f4f7d101e62..7c97b14ea78 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -58,6 +58,7 @@ public void Setup() _physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); _mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + _gridUid = gridEnt.Owner; } @@ -85,7 +86,7 @@ public EntityUid SetupTestObject(EntityCoordinates coords) new(-0.1f, -0.1f), }); _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false), manager: objFix, body: objPhys); - _physSystem.WakeBody(obj, manager: objFix, body: objPhys); + _physSystem.SetCanCollide(obj, true, body: objPhys); return obj; } @@ -132,7 +133,7 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() public async Task TestLinearVelocityOnlyMoveOntoGrid() { // Spawn our test object 1 m off of the middle of the grid in both directions. - _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.0f, 1.0f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); Assert.Multiple(() => { @@ -193,7 +194,7 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() public async Task TestLinearAndAngularVelocityMoveOntoGrid() { // Spawn our test object 1 m off of the middle of the grid in both directions. - _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.0f, 1.0f)); + _objUid = SetupTestObject(new EntityCoordinates(_mapUid, 1.5f, 1.5f)); Assert.Multiple(() => { From ecafcdaba14edcef15c89bc4fafd563b891debb5 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:15:29 -0400 Subject: [PATCH 38/45] set fixture as box --- .../Shared/Physics/GridReparentVelocity_Test.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 7c97b14ea78..c7b4459ceef 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -78,13 +78,7 @@ public EntityUid SetupTestObject(EntityCoordinates coords) // Set up fixture. var poly = new PolygonShape(); - poly.Set(new List() - { - new(0.1f, -0.1f), - new(0.1f, 0.1f), - new(-0.1f, 0.1f), - new(-0.1f, -0.1f), - }); + poly.SetAsBox(0.1f, 0.1f); _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false), manager: objFix, body: objPhys); _physSystem.SetCanCollide(obj, true, body: objPhys); From ef0659d239474aed10332d12c7ce4cd8966430ea Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:27:14 -0400 Subject: [PATCH 39/45] whitespace --- .../Shared/Physics/GridReparentVelocity_Test.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index c7b4459ceef..ad45a8ecdaa 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; using NUnit.Framework; @@ -105,7 +104,7 @@ public async Task TestLinearVelocityOnlyMoveOffGrid() { // Our object should start on the grid. Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - + // Set the velocity of the grid and our object. Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(1.0f, 2.0f)), Is.True); @@ -133,7 +132,7 @@ public async Task TestLinearVelocityOnlyMoveOntoGrid() { // Assert that we start off the grid. Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - + // Set the velocity of the grid and our object. Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); Assert.That(_physSystem.SetLinearVelocity(_gridUid, new Vector2(-1.0f, -2.0f)), Is.True); @@ -161,7 +160,7 @@ public async Task TestLinearAndAngularVelocityMoveOffGrid() { // Our object should start on the grid. Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_gridUid)); - + // Set the velocity of the grid and our object. Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(3.5f, 4.75f)), Is.True); Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); @@ -194,7 +193,7 @@ public async Task TestLinearAndAngularVelocityMoveOntoGrid() { // Assert that we start off the grid. Assert.That(_entManager.GetComponent(_objUid).ParentUid, Is.EqualTo(_mapUid)); - + // Set the velocity of the grid and our object. Assert.That(_physSystem.SetLinearVelocity(_objUid, new Vector2(-2.0f, -3.0f)), Is.True); Assert.That(_physSystem.SetAngularVelocity(_objUid, 1.0f), Is.True); From 223f81d1ab9277d226317caeda07dd79aa1adece Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:32:50 -0400 Subject: [PATCH 40/45] aabb --- .../Shared/Physics/GridReparentVelocity_Test.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index ad45a8ecdaa..f6869a0abd9 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -76,9 +76,8 @@ public EntityUid SetupTestObject(EntityCoordinates coords) _physSystem.SetAngularDamping(obj, objPhys, 0.0f); // Set up fixture. - var poly = new PolygonShape(); - poly.SetAsBox(0.1f, 0.1f); - _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false), manager: objFix, body: objPhys); + var aabb = new PhysShapeAabb(0.1f); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(aabb, 0, 0, false), manager: objFix, body: objPhys); _physSystem.SetCanCollide(obj, true, body: objPhys); return obj; From 635299fd71f286c681de817ee96863bd0fc8e2f4 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:48:12 -0400 Subject: [PATCH 41/45] whitespace --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index f6869a0abd9..bd7a35aea13 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -42,7 +42,7 @@ public void FixtureSetup() _fixtureSystem = _systems.GetEntitySystem(); _mapSystem = _systems.GetEntitySystem(); _physSystem = _systems.GetEntitySystem(); - + _mapUid = _mapSystem.CreateMap(out _mapId); } From fd8d16a4802f9103e10c698e2aa7faaa85f71843 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 00:56:24 -0400 Subject: [PATCH 42/45] unparent RobustIntegrationTest --- Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index bd7a35aea13..12fc657ddf0 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -14,7 +14,7 @@ namespace Robust.UnitTesting.Shared.Physics; [TestFixture, TestOf(typeof(SharedPhysicsSystem))] -public sealed class GridReparentVelocity_Test : RobustIntegrationTest +public sealed class GridReparentVelocity_Test { private ISimulation _sim = default!; private IEntitySystemManager _systems = default!; From 259f82275769bf99c9d3b856061bf6e1f0287e27 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 01:10:46 -0400 Subject: [PATCH 43/45] wake me up --- .../Shared/Physics/GridReparentVelocity_Test.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 12fc657ddf0..28e73642f21 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -52,11 +52,13 @@ public void Setup() // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. var gridEnt = _mapManager.CreateGridEntity(_mapId); var gridPhys = _entManager.GetComponent(gridEnt); + _physSystem.SetSleepingAllowed(gridEnt, gridPhys, false); _physSystem.SetBodyType(gridEnt, BodyType.Dynamic, body: gridPhys); _physSystem.SetLinearDamping(gridEnt, gridPhys, 0.0f); _physSystem.SetAngularDamping(gridEnt, gridPhys, 0.0f); _mapSystem.SetTile(gridEnt, Vector2i.Zero, new Tile(1)); + _physSystem.WakeBody(gridEnt, body: gridPhys); _gridUid = gridEnt.Owner; } @@ -71,6 +73,7 @@ public EntityUid SetupTestObject(EntityCoordinates coords) // Set up physics (no velocity damping, dynamic body, physics enabled) _entManager.GetComponent(obj); + _physSystem.SetSleepingAllowed(obj, objPhys, false); _physSystem.SetBodyType(obj, BodyType.Dynamic, body: objPhys); _physSystem.SetLinearDamping(obj, objPhys, 0.0f); _physSystem.SetAngularDamping(obj, objPhys, 0.0f); @@ -78,7 +81,7 @@ public EntityUid SetupTestObject(EntityCoordinates coords) // Set up fixture. var aabb = new PhysShapeAabb(0.1f); _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(aabb, 0, 0, false), manager: objFix, body: objPhys); - _physSystem.SetCanCollide(obj, true, body: objPhys); + _physSystem.WakeBody(obj, body: objPhys); return obj; } From 26347976ba8397f6faba7b37c8c777933753f980 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 01:33:25 -0400 Subject: [PATCH 44/45] restore the polygon --- .../Shared/Physics/GridReparentVelocity_Test.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index 28e73642f21..bd0b09a4f54 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -79,8 +79,9 @@ public EntityUid SetupTestObject(EntityCoordinates coords) _physSystem.SetAngularDamping(obj, objPhys, 0.0f); // Set up fixture. - var aabb = new PhysShapeAabb(0.1f); - _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(aabb, 0, 0, false), manager: objFix, body: objPhys); + var poly = new PolygonShape(); + poly.SetAsBox(0.1f, 0.1f); + _fixtureSystem.CreateFixture(obj, "fix1", new Fixture(poly, 0, 0, false), manager: objFix, body: objPhys); _physSystem.WakeBody(obj, body: objPhys); return obj; From d863e684738a0525e96f42d7e96408428830196e Mon Sep 17 00:00:00 2001 From: Whatstone Date: Fri, 4 Apr 2025 02:03:21 -0400 Subject: [PATCH 45/45] teardown map per test --- .../Shared/Physics/GridReparentVelocity_Test.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs index bd0b09a4f54..fe0038f28b0 100644 --- a/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/GridReparentVelocity_Test.cs @@ -42,13 +42,13 @@ public void FixtureSetup() _fixtureSystem = _systems.GetEntitySystem(); _mapSystem = _systems.GetEntitySystem(); _physSystem = _systems.GetEntitySystem(); - - _mapUid = _mapSystem.CreateMap(out _mapId); } [SetUp] public void Setup() { + _mapUid = _mapSystem.CreateMap(out _mapId); + // Spawn a 1x1 grid centered at (0.5, 0.5), ensure it's movable and its velocity has no damping. var gridEnt = _mapManager.CreateGridEntity(_mapId); var gridPhys = _entManager.GetComponent(gridEnt); @@ -94,6 +94,9 @@ public void Teardown() _gridUid = default!; _entManager.DeleteEntity(_objUid); _objUid = default!; + _mapSystem.DeleteMap(_mapId); + _mapId = default!; + _entManager.DeleteEntity(_mapUid); } // Moves an object off of a moving grid, checks for conservation of linear velocity.