diff --git a/Robust.Shared/Physics/Collision/Shapes/PolygonShape.cs b/Robust.Shared/Physics/Collision/Shapes/PolygonShape.cs index a6b00d1f599..240be70ed20 100644 --- a/Robust.Shared/Physics/Collision/Shapes/PolygonShape.cs +++ b/Robust.Shared/Physics/Collision/Shapes/PolygonShape.cs @@ -27,6 +27,7 @@ using Robust.Shared.Configuration; using Robust.Shared.IoC; using Robust.Shared.Maths; +using Robust.Shared.Physics.Shapes; using Robust.Shared.Physics.Systems; using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager.Attributes; @@ -172,6 +173,13 @@ public PolygonShape() { } + internal PolygonShape(Polygon poly) + { + Vertices = poly.Vertices; + Normals = poly.Normals; + Centroid = poly.Centroid; + } + public PolygonShape(float radius) { Radius = radius; diff --git a/Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs b/Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs index e3c7bfec656..e11e7b7b776 100644 --- a/Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs +++ b/Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs @@ -22,12 +22,9 @@ public sealed partial class RayCastSystem /// private static float RayCastAllCallback(FixtureProxy proxy, Vector2 point, Vector2 normal, float fraction, ref RayResult result) { - result.Results.Add(new RayHit() + result.Results.Add(new RayHit(proxy.Entity, normal, fraction) { - Fraction = fraction, - Normal = normal, Point = point, - Proxy = proxy, }); return 1f; } @@ -54,12 +51,9 @@ private static float RayCastClosestCallback(FixtureProxy proxy, Vector2 point, V if (add) { - result.Results.Add(new RayHit() + result.Results.Add(new RayHit(proxy.Entity, normal, fraction) { - Fraction = fraction, - Normal = normal, Point = point, - Proxy = proxy, }); } @@ -646,16 +640,6 @@ private CastOutput ShapeCastSegment(ShapeCastInput input, EdgeShape shape) } #endregion - - private ref struct RayCastQueryState - { - public RayCastSystem System; - public SharedPhysicsSystem Physics; - - public uint CollisionMask; - public Vector2 Origin; - public Vector2 Translation; - } } internal ref struct WorldRayCastContext diff --git a/Robust.Shared/Physics/Systems/RayCastSystem.cs b/Robust.Shared/Physics/Systems/RayCastSystem.cs index 62f09493acf..cfd005d5956 100644 --- a/Robust.Shared/Physics/Systems/RayCastSystem.cs +++ b/Robust.Shared/Physics/Systems/RayCastSystem.cs @@ -269,8 +269,11 @@ public void CastShape( case PhysShapeCircle circle: CastCircle(entity, ref result, circle, originTransform, translation, filter); break; + case Polygon poly: + CastPolygon(entity, ref result, new PolygonShape(poly), originTransform, translation, filter); + break; case PolygonShape polygon: - CastPolygon(entity, ref result,polygon, originTransform, translation, filter); + CastPolygon(entity, ref result, polygon, originTransform, translation, filter); break; default: Log.Error("Tried to shapecast for shape not implemented."); @@ -371,12 +374,15 @@ public record struct RayResult() public bool Hit => Results.Count > 0; } -public record struct RayHit() +public record struct RayHit(EntityUid Entity, Vector2 LocalNormal, float Fraction) { - public FixtureProxy Proxy; + public readonly EntityUid Entity = Entity; + public readonly Vector2 LocalNormal = LocalNormal; + public readonly float Fraction = Fraction; + + // When this point gets added it's in broadphase terms, then the caller handles whether it gets turned into map-terms. + public Vector2 Point; - public Vector2 Normal; - public float Fraction; } /// The query filter is used to filter collisions between queries and shapes. For example, diff --git a/Robust.UnitTesting/Shared/Physics/RayCast_Test.cs b/Robust.UnitTesting/Shared/Physics/RayCast_Test.cs index f25896e1501..09ee51e0feb 100644 --- a/Robust.UnitTesting/Shared/Physics/RayCast_Test.cs +++ b/Robust.UnitTesting/Shared/Physics/RayCast_Test.cs @@ -25,6 +25,9 @@ public sealed class RayCast_Test // Ray stops inside new(new Vector2(0f, 0.5f), Vector2.UnitY, new Vector2(0f, 1f - PhysicsConstants.PolygonRadius)), + // Ray starts inside + new(new Vector2(0f, 1.5f), Vector2.UnitY, null), + // No hit new(new Vector2(0f, 0.5f), -Vector2.UnitY, null), }; @@ -32,13 +35,18 @@ public sealed class RayCast_Test private static TestCaseData[] _shapeCases = { // Circle - // - Initial overlap - new(new PhysShapeCircle(0.5f, Vector2.Zero), new Transform(Vector2.UnitY / 2f, Angle.Zero), Vector2.UnitY, new Vector2(0f, 0.5f)), + // - Initial overlap, no shapecast + new(new PhysShapeCircle(0.5f, Vector2.Zero), new Transform(Vector2.UnitY / 2f, Angle.Zero), Vector2.UnitY, null), // - Cast new(new PhysShapeCircle(0.5f, Vector2.Zero), new Transform(Vector2.Zero, Angle.Zero), Vector2.UnitY, new Vector2(0f, 1f - PhysicsConstants.PolygonRadius)), // Polygon + // - Initial overlap, no shapecast + new(new Polygon(Box2.UnitCentered), new Transform(Vector2.UnitY / 2f, Angle.Zero), Vector2.UnitY, null), + + // - Cast + new(new Polygon(Box2.UnitCentered), new Transform(Vector2.Zero, Angle.Zero), Vector2.UnitY, new Vector2(0.5f, 1f - PhysicsConstants.PolygonRadius)), }; [Test, TestCaseSource(nameof(_rayCases))]