feat: Shape to Vector2D Overlap
This commit is contained in:
parent
72492a9f5a
commit
b931abb735
|
@ -1,13 +1,33 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Physics2D;
|
|
||||||
using Syntriax.Engine.Physics2D.Primitives;
|
using Syntriax.Engine.Physics2D.Primitives;
|
||||||
|
|
||||||
namespace Engine.Physics2D;
|
namespace Engine.Physics2D;
|
||||||
|
|
||||||
public static partial class 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)
|
public static bool Overlaps(this Circle left, Circle right)
|
||||||
{
|
{
|
||||||
float distanceSquared = left.Center.FromTo(right.Center).LengthSquared();
|
float distanceSquared = left.Center.FromTo(right.Center).LengthSquared();
|
||||||
|
@ -27,7 +47,7 @@ public static partial class Physics2D
|
||||||
normal = distanceVector.Normalized;
|
normal = distanceVector.Normalized;
|
||||||
|
|
||||||
if (isOverlapping)
|
if (isOverlapping)
|
||||||
depth = MathF.Sqrt(radiusSumSquared - distanceSquared);
|
depth = Math.Sqrt(radiusSumSquared - distanceSquared);
|
||||||
|
|
||||||
return isOverlapping;
|
return isOverlapping;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +64,7 @@ public static partial class Physics2D
|
||||||
normal = distanceVector.Normalized;
|
normal = distanceVector.Normalized;
|
||||||
|
|
||||||
if (isOverlapping)
|
if (isOverlapping)
|
||||||
depth = MathF.Sqrt(radiusSquared - distanceSquared);
|
depth = Math.Sqrt(radiusSquared - distanceSquared);
|
||||||
|
|
||||||
return isOverlapping;
|
return isOverlapping;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue