Skip to content

Commit

Permalink
free
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Sep 24, 2024
1 parent 51eb9d0 commit d1a33f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
8 changes: 8 additions & 0 deletions Robust.Shared/Physics/Collision/Shapes/PolygonShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 2 additions & 18 deletions Robust.Shared/Physics/Systems/RayCastSystem.Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public sealed partial class RayCastSystem
/// </summary>
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;
}
Expand All @@ -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,
});
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions Robust.Shared/Physics/Systems/RayCastSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions Robust.UnitTesting/Shared/Physics/RayCast_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ 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),
};

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))]
Expand Down

0 comments on commit d1a33f8

Please sign in to comment.