Syntriax.Engine/Engine.Physics2D/Primitives/Projection.cs

50 lines
1.6 KiB
C#
Raw Normal View History

2024-01-26 12:39:37 +03:00
namespace Syntriax.Engine.Physics2D.Primitives;
[System.Diagnostics.DebuggerDisplay("Min: {Min}, Max: {Max}")]
public readonly struct Projection(float Min, float Max)
2024-01-26 12:39:37 +03:00
{
public readonly float Min { get; init; } = Min;
public readonly float Max { get; init; } = Max;
2024-01-26 12:39:37 +03:00
public static bool Overlaps(Projection left, Projection right) => Overlaps(left, right, out var _);
public static bool Overlaps(Projection left, Projection right, out float depth)
{
2024-01-26 19:13:53 +03:00
// TODO Try to improve this
2024-01-26 12:39:37 +03:00
bool rightMinInLeft = right.Min > left.Min && right.Min < left.Max;
if (rightMinInLeft)
{
depth = left.Max - right.Min;
return true;
}
bool rightMaxInLeft = right.Max < left.Max && right.Max > left.Min;
if (rightMaxInLeft)
{
depth = left.Min - right.Max;
return true;
}
2024-01-26 19:13:53 +03:00
bool leftMinInRight = left.Min > right.Min && left.Min < right.Max;
if (leftMinInRight)
{
depth = right.Max - left.Min;
return true;
}
bool leftMaxInRight = left.Max < right.Max && left.Max > right.Min;
if (leftMaxInRight)
{
depth = right.Min - left.Max;
return true;
}
depth = 0f;
2024-01-26 12:39:37 +03:00
return false;
}
}
public static class ProjectionExtensions
{
public static bool Overlaps(this Projection left, Projection right) => Projection.Overlaps(left, right);
public static bool Overlaps(this Projection left, Projection right, out float depth) => Projection.Overlaps(left, right, out depth);
}