feat: Shape to Vector2D Overlap

This commit is contained in:
Syntriax 2024-02-06 12:06:13 +03:00
parent 72492a9f5a
commit b931abb735
1 changed files with 25 additions and 5 deletions

View File

@ -1,13 +1,33 @@
using System;
using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Primitives;
namespace Engine.Physics2D;
public static partial class Physics2D
{
public static bool Overlaps(this Shape shape, Vector2D point) => Overlaps(shape, point, out var _);
public static bool Overlaps(this Shape shape, Vector2D point, out float depth)
{
depth = float.MaxValue;
var vertices = shape.Vertices;
int count = vertices.Count;
for (int indexProjection = 0; indexProjection < count; indexProjection++)
{
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
Projection shapeProjection = shape.ToProjection(projectionVector);
float projectedPoint = point.Dot(projectionVector);
if (shapeProjection.Max < projectedPoint || shapeProjection.Min > projectedPoint)
return false;
depth = Math.Min(depth, Math.Abs(Math.AbsMin(shapeProjection.Max - projectedPoint, shapeProjection.Min - projectedPoint)));
}
return true;
}
public static bool Overlaps(this Circle left, Circle right)
{
float distanceSquared = left.Center.FromTo(right.Center).LengthSquared();
@ -27,7 +47,7 @@ public static partial class Physics2D
normal = distanceVector.Normalized;
if (isOverlapping)
depth = MathF.Sqrt(radiusSumSquared - distanceSquared);
depth = Math.Sqrt(radiusSumSquared - distanceSquared);
return isOverlapping;
}
@ -44,7 +64,7 @@ public static partial class Physics2D
normal = distanceVector.Normalized;
if (isOverlapping)
depth = MathF.Sqrt(radiusSquared - distanceSquared);
depth = Math.Sqrt(radiusSquared - distanceSquared);
return isOverlapping;
}