2024-01-26 12:39:37 +03:00
|
|
|
namespace Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
|
2024-02-01 12:32:58 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Represents a range of values along a single axis.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="min">The minimum value of the projection.</param>
|
|
|
|
/// <param name="max">The maximum value of the projection.</param>
|
|
|
|
/// <remarks>
|
|
|
|
/// Initializes a new instance of the <see cref="Projection"/> struct with the specified minimum and maximum values.
|
|
|
|
/// </remarks>
|
2024-01-27 00:51:34 +03:00
|
|
|
[System.Diagnostics.DebuggerDisplay("Min: {Min}, Max: {Max}")]
|
2024-02-01 12:32:58 +03:00
|
|
|
public readonly struct Projection(float min, float max)
|
2024-01-26 12:39:37 +03:00
|
|
|
{
|
2024-02-01 12:32:58 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the minimum value of the projection.
|
|
|
|
/// </summary>
|
|
|
|
public readonly float Min = min;
|
2024-01-26 23:40:02 +03:00
|
|
|
|
2024-02-01 12:32:58 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the maximum value of the projection.
|
|
|
|
/// </summary>
|
|
|
|
public readonly float Max = max;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if two projections overlap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first projection to check.</param>
|
|
|
|
/// <param name="right">The second projection to check.</param>
|
|
|
|
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
2024-01-26 12:39:37 +03:00
|
|
|
public static bool Overlaps(Projection left, Projection right) => Overlaps(left, right, out var _);
|
2024-02-01 12:32:58 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if two projections overlap and calculates the depth of the overlap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first projection to check.</param>
|
|
|
|
/// <param name="right">The second projection to check.</param>
|
|
|
|
/// <param name="depth">The depth of the overlap, if any.</param>
|
|
|
|
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
2024-01-26 12:39:37 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-02-01 12:32:58 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Provides extension methods for the <see cref="Projection"/> struct.
|
|
|
|
/// </summary>
|
2024-01-26 12:39:37 +03:00
|
|
|
public static class ProjectionExtensions
|
|
|
|
{
|
2024-02-01 12:32:58 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if two projections overlap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first projection to check.</param>
|
|
|
|
/// <param name="right">The second projection to check.</param>
|
|
|
|
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
2024-01-26 12:39:37 +03:00
|
|
|
public static bool Overlaps(this Projection left, Projection right) => Projection.Overlaps(left, right);
|
2024-02-01 12:32:58 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if two projections overlap and calculates the depth of the overlap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first projection to check.</param>
|
|
|
|
/// <param name="right">The second projection to check.</param>
|
|
|
|
/// <param name="depth">The depth of the overlap, if any.</param>
|
|
|
|
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
2024-01-26 12:39:37 +03:00
|
|
|
public static bool Overlaps(this Projection left, Projection right, out float depth) => Projection.Overlaps(left, right, out depth);
|
|
|
|
}
|