From 8ebde9dedf34e892afa61e6d0720685d9353ad32 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 26 Jan 2024 12:39:37 +0300 Subject: [PATCH] feat: Projection Data Record --- Engine.Physics2D/Primitives/Projection.cs | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Engine.Physics2D/Primitives/Projection.cs diff --git a/Engine.Physics2D/Primitives/Projection.cs b/Engine.Physics2D/Primitives/Projection.cs new file mode 100644 index 0000000..acbecc8 --- /dev/null +++ b/Engine.Physics2D/Primitives/Projection.cs @@ -0,0 +1,30 @@ +namespace Syntriax.Engine.Physics2D.Primitives; + +public record Projection(float Min, float Max) +{ + public static bool Overlaps(Projection left, Projection right) => Overlaps(left, right, out var _); + public static bool Overlaps(Projection left, Projection right, out float depth) + { + 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; + } + + depth = default; + 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); +}