From 85bad951ff9802d1b8c0a00e23f11914b5acd20d Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 26 Jan 2024 19:13:53 +0300 Subject: [PATCH] fix: Shape Collision on Larger Shapes --- Engine.Physics2D/Primitives/Projection.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Engine.Physics2D/Primitives/Projection.cs b/Engine.Physics2D/Primitives/Projection.cs index acbecc8..153c4c4 100644 --- a/Engine.Physics2D/Primitives/Projection.cs +++ b/Engine.Physics2D/Primitives/Projection.cs @@ -5,6 +5,7 @@ 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) { + // TODO Try to improve this bool rightMinInLeft = right.Min > left.Min && right.Min < left.Max; if (rightMinInLeft) { @@ -19,7 +20,21 @@ public record Projection(float Min, float Max) return true; } - depth = default; + 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; return false; } }