// This function calculate Circle to ABB collisions public static CollisionInfo CircleToOBBCollision(CollisionHull2D a, CollisionHull2D b) { Vector2 closestPointToCircle = new Vector2(Math.Max(b.GetMinimumCorner().x, Math.Min(a.GetPosition().x, b.GetMaximumCorner().x)), Math.Max(b.GetMinimumCorner().y, Math.Min(a.GetPosition().y, b.GetMaximumCorner().y))); Vector2 distance = a.GetPosition() - closestPointToCircle; float distanceSquared = Vector2.Dot(distance, distance); float penetration = a.GetDimensions().x - Mathf.Sqrt(distanceSquared); // Does the check pass? if (penetration > 0) { // If yes, then inform the parents of the complex shape object (if applicable) ReportCollisionToParent(a, b); } else { // If no, return nothing return(null); } // Return full details of the Collision list if the two collide return(new CollisionInfo(a, b, penetration)); }
// This function calculates Circle to OBB collisions public static CollisionInfo CircleToABBCollision(CollisionHull2D a, CollisionHull2D b) { // Find the closest point to the circle from the AABB Vector2 closestPointToCircle = new Vector2(Math.Max(b.GetMinimumCorner().x, Math.Min(a.GetPosition().x, b.GetMaximumCorner().x)), Math.Max(b.GetMinimumCorner().y, Math.Min(a.GetPosition().y, b.GetMaximumCorner().y))); // Get the distance between the closest point and the circle's position Vector2 distance = a.GetPosition() - closestPointToCircle; float distanceSquared = Vector2.Dot(distance, distance); // Calculate the penetration float penetration = a.GetDimensions().x - Mathf.Sqrt(distanceSquared); // Is the penetration a positive value if (penetration > 0) { // If yes, then inform the parents of the complex shape object (if applicable) ReportCollisionToParent(a, b); } else { // If no, return nothing return(null); } // Return full details of the Collision list if the two collide return(new CollisionInfo(a, b, penetration)); }