2024-01-23 19:18:31 +03:00
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
|
|
|
|
namespace Engine.Physics2D;
|
|
|
|
|
|
|
|
public static partial class Physics2D
|
|
|
|
{
|
2024-02-06 12:06:13 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:18:31 +03:00
|
|
|
public static bool Overlaps(this Circle left, Circle right)
|
|
|
|
{
|
2024-01-24 19:21:53 +03:00
|
|
|
float distanceSquared = left.Center.FromTo(right.Center).LengthSquared();
|
2024-01-23 19:18:31 +03:00
|
|
|
float radiusSumSquared = left.RadiusSquared + right.RadiusSquared;
|
|
|
|
|
|
|
|
return distanceSquared < radiusSumSquared;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool Overlaps(this Circle left, Circle right, out Vector2D normal, out float depth)
|
|
|
|
{
|
2024-01-24 19:21:53 +03:00
|
|
|
Vector2D distanceVector = left.Center.FromTo(right.Center);
|
2024-01-23 19:18:31 +03:00
|
|
|
float distanceSquared = distanceVector.LengthSquared();
|
|
|
|
float radiusSumSquared = left.RadiusSquared + right.RadiusSquared;
|
|
|
|
bool isOverlapping = distanceSquared < radiusSumSquared;
|
|
|
|
|
|
|
|
depth = 0f;
|
|
|
|
normal = distanceVector.Normalized;
|
|
|
|
|
|
|
|
if (isOverlapping)
|
2024-02-06 12:06:13 +03:00
|
|
|
depth = Math.Sqrt(radiusSumSquared - distanceSquared);
|
2024-01-23 19:18:31 +03:00
|
|
|
|
|
|
|
return isOverlapping;
|
|
|
|
}
|
|
|
|
|
2024-01-24 19:21:53 +03:00
|
|
|
public static bool Overlaps(this Circle circle, Vector2D point) => circle.Center.FromTo(point).LengthSquared() <= circle.RadiusSquared;
|
2024-01-23 19:18:31 +03:00
|
|
|
public static bool Overlaps(this Circle circle, Vector2D point, out Vector2D normal, out float depth)
|
|
|
|
{
|
2024-01-24 19:21:53 +03:00
|
|
|
Vector2D distanceVector = circle.Center.FromTo(point);
|
2024-01-23 19:18:31 +03:00
|
|
|
float distanceSquared = distanceVector.LengthSquared();
|
|
|
|
float radiusSquared = circle.RadiusSquared;
|
|
|
|
bool isOverlapping = distanceSquared < radiusSquared;
|
|
|
|
|
|
|
|
depth = 0f;
|
|
|
|
normal = distanceVector.Normalized;
|
|
|
|
|
|
|
|
if (isOverlapping)
|
2024-02-06 12:06:13 +03:00
|
|
|
depth = Math.Sqrt(radiusSquared - distanceSquared);
|
2024-01-23 19:18:31 +03:00
|
|
|
|
|
|
|
return isOverlapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool Overlaps(this AABB aabb, Vector2D point)
|
|
|
|
=> point.X >= aabb.LowerBoundary.X && point.X <= aabb.UpperBoundary.X &&
|
|
|
|
point.Y >= aabb.LowerBoundary.Y && point.Y <= aabb.UpperBoundary.Y;
|
|
|
|
|
|
|
|
public static bool Overlaps(this AABB left, AABB right)
|
|
|
|
=> left.LowerBoundary.X <= right.UpperBoundary.X && left.UpperBoundary.X >= right.LowerBoundary.X &&
|
|
|
|
left.LowerBoundary.Y <= right.UpperBoundary.Y && left.UpperBoundary.Y >= right.LowerBoundary.Y;
|
|
|
|
|
|
|
|
public static bool Overlaps(Triangle triangle, Vector2D point)
|
|
|
|
{
|
|
|
|
float originalTriangleArea = triangle.Area;
|
|
|
|
|
|
|
|
float pointTriangleArea1 = new Triangle(point, triangle.B, triangle.C).Area;
|
|
|
|
float pointTriangleArea2 = new Triangle(triangle.A, point, triangle.C).Area;
|
|
|
|
float pointTriangleArea3 = new Triangle(triangle.A, triangle.B, point).Area;
|
|
|
|
|
|
|
|
float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3;
|
|
|
|
|
|
|
|
return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool LaysOn(this Vector2D point, Line line) => Line.Intersects(line, point);
|
|
|
|
}
|