diff --git a/Engine.Physics2D/CollisionDetectionInformation.cs b/Engine.Physics2D/CollisionDetectionInformation.cs index 453932c..3a330dd 100644 --- a/Engine.Physics2D/CollisionDetectionInformation.cs +++ b/Engine.Physics2D/CollisionDetectionInformation.cs @@ -6,14 +6,16 @@ namespace Syntriax.Engine.Physics2D; [System.Diagnostics.DebuggerDisplay("Normal: {Normal.ToString(), nq}, Penetration: {Penetration}")] public readonly struct CollisionDetectionInformation ( - ICollider2D Left, - ICollider2D Right, + ICollider2D Detector, + ICollider2D Detected, Vector2D Normal, float Penetration ) { - public ICollider2D Left { get; init; } = Left; - public ICollider2D Right { get; init; } = Right; + public ICollider2D Detector { get; init; } = Detector; + public ICollider2D Detected { get; init; } = Detected; public Vector2D Normal { get; init; } = Normal; public float Penetration { get; init; } = Penetration; + + public CollisionDetectionInformation Reverse() => new(Detected, Detector, -Normal, Penetration); } diff --git a/Engine.Physics2D/CollisionDetector2D.cs b/Engine.Physics2D/CollisionDetector2D.cs index 1f9c332..b3e1643 100644 --- a/Engine.Physics2D/CollisionDetector2D.cs +++ b/Engine.Physics2D/CollisionDetector2D.cs @@ -55,7 +55,7 @@ public class CollisionDetector2D : ICollisionDetector2D if (!leftProjection.Overlaps(rightProjection, out float depth)) return false; - if (collisionInformation.Left is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) + if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) collisionInformation = new(left, right, projectionVector, depth); } @@ -79,7 +79,7 @@ public class CollisionDetector2D : ICollisionDetector2D if (!shapeProjection.Overlaps(circleProjection, out float depth)) return false; - if (collisionInformation.Left is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) + if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) collisionInformation = new(shapeCollider, circleCollider, projectionVector, depth); } @@ -92,7 +92,7 @@ public class CollisionDetector2D : ICollisionDetector2D if (!shapeProjection.Overlaps(circleProjection, out float depth)) return false; - if (collisionInformation.Left is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) + if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth)) collisionInformation = new(shapeCollider, circleCollider, shapeToCircleProjectionVector, depth); } diff --git a/Engine.Physics2D/CollisionResolver2D.cs b/Engine.Physics2D/CollisionResolver2D.cs index a55b7d5..29e031b 100644 --- a/Engine.Physics2D/CollisionResolver2D.cs +++ b/Engine.Physics2D/CollisionResolver2D.cs @@ -9,8 +9,8 @@ public class CollisionResolver2D : ICollisionResolver2D { Vector2D displacementVector = collisionInformation.Normal * collisionInformation.Penetration; - ICollider2D left = collisionInformation.Left; - ICollider2D right = collisionInformation.Right; + ICollider2D left = collisionInformation.Detector; + ICollider2D right = collisionInformation.Detected; bool isLeftStatic = left.RigidBody2D?.IsStatic ?? true; bool isRightStatic = right.RigidBody2D?.IsStatic ?? true; diff --git a/Engine.Physics2D/PhysicsEngine2D.cs b/Engine.Physics2D/PhysicsEngine2D.cs index bbe6f59..7a5b8f2 100644 --- a/Engine.Physics2D/PhysicsEngine2D.cs +++ b/Engine.Physics2D/PhysicsEngine2D.cs @@ -91,8 +91,16 @@ public class PhysicsEngine2D : IPhysicsEngine2D continue; } - colliderX.Detect(information); - colliderY.Detect(information); + if (information.Detector == colliderX) + { + colliderX.Detect(information); + colliderY.Detect(information.Reverse()); + } + else + { + colliderX.Detect(information.Reverse()); + colliderY.Detect(information); + } collisionResolver?.Resolve(information); } diff --git a/Engine.Physics2D/PhysicsEngine2DCollector.cs b/Engine.Physics2D/PhysicsEngine2DCollector.cs index 0cc06f2..886cdc4 100644 --- a/Engine.Physics2D/PhysicsEngine2DCollector.cs +++ b/Engine.Physics2D/PhysicsEngine2DCollector.cs @@ -73,8 +73,16 @@ public class PhysicsEngine2DCollector : HierarchyObjectBase, IPhysicsEngine2D continue; } - colliderX.Detect(information); - colliderY.Detect(information); + if (information.Detector == colliderX) + { + colliderX.Detect(information); + colliderY.Detect(information.Reverse()); + } + else + { + colliderX.Detect(information.Reverse()); + colliderY.Detect(information); + } collisionResolver?.Resolve(information); }