protected override CollisionResult ContactWithRectangle(ConvexRect otherRect) { bool collision = true; //this rectangle's normal of 4 corner and use it as projection axis Vector2[] rect1Normals = this.Normals; Vector2[] rect1AllCorners = this.AllCorners; Vector2[] rect2AllCorners = otherRect.AllCorners; //For each normals in this rectangle for (int i = 0; i < rect1Normals.Length; i++) { //Projecting all corners from rect1 to rect1's normal float r1Dot1 = Vector2.Dot(rect1Normals [i], rect1AllCorners [0]); float r1Dot2 = Vector2.Dot(rect1Normals [i], rect1AllCorners [1]); float r1Dot3 = Vector2.Dot(rect1Normals [i], rect1AllCorners [2]); float r1Dot4 = Vector2.Dot(rect1Normals [i], rect1AllCorners [3]); //Find rect1 max and min projection float r1PMin = Mathf.Min(r1Dot1, Mathf.Min(r1Dot2, Mathf.Min(r1Dot3, r1Dot4))); float r1PMax = Mathf.Max(r1Dot1, Mathf.Max(r1Dot2, Mathf.Max(r1Dot3, r1Dot4))); //Projecting all corners from rect2 to rect1's normal float r2Dot1 = Vector2.Dot(rect1Normals [i], rect2AllCorners [0]); float r2Dot2 = Vector2.Dot(rect1Normals [i], rect2AllCorners [1]); float r2Dot3 = Vector2.Dot(rect1Normals [i], rect2AllCorners [2]); float r2Dot4 = Vector2.Dot(rect1Normals [i], rect2AllCorners [3]); //Find rect2 max and min projection float r2PMin = Mathf.Min(r2Dot1, Mathf.Min(r2Dot2, Mathf.Min(r2Dot3, r2Dot4))); float r2PMax = Mathf.Max(r2Dot1, Mathf.Max(r2Dot2, Mathf.Max(r2Dot3, r2Dot4))); //Two rectangles not collide each other if there is a gap //and we do not check further if (r2PMin > r1PMax || r2PMax < r1PMin) { collision = false; break; } } //Check if this rectangle is inside another rectangle if (collision == true) { bool inside = true; //4 corners of this rectangle foreach (Vector2 corner in AllCorners) { //if other rectangle not contain this corner if (!otherRect.ContainPoint2D(corner)) { inside = false; break; } } if (inside == true) { return(CollisionResult.Fit); } return(CollisionResult.Overlap); } //There is no collision between two rectangles; return(CollisionResult.None); }