void CheckCollision() { Debug.Log(rect2.IntersectWithShape(rect1)); /* * //projection axis * Vector2 axisP = new Vector2 (1.0f, 0.0f); * Vector2 axisQ = new Vector2 (0.0f, 1.0f); * * //max point of rect * Vector2 rect1MaxPoint = new Vector2 (rect1.xMax, rect1.yMax); * Vector2 rect2MaxPoint = new Vector2 (rect2.xMax, rect2.yMax); * Vector2 rect1MinPoint = new Vector2 (rect1.xMin, rect1.yMin); * Vector2 rect2MinPoint = new Vector2 (rect2.xMin, rect2.yMin); * * //Vector of each max point from world space origin * Vector2 vMaxRect1 = rect1MaxPoint - Vector2.zero; * Vector2 vMaxRect2 = rect2MaxPoint - Vector2.zero; * Vector2 vMinRect1 = rect1MinPoint - Vector2.zero; * Vector2 vMinRect2 = rect2MinPoint - Vector2.zero; * * //project vector on x axis * float maxProjRect1X = Vector2.Dot (vMaxRect1, axisP); * float maxProjRect2X = Vector2.Dot (vMaxRect2, axisP); * float minProjRect1X = Vector2.Dot (vMinRect1, axisP); * float minProjRect2X = Vector2.Dot (vMinRect2, axisP); * * bool gapX = maxProjRect1X < minProjRect2X || minProjRect1X > maxProjRect2X; * * * //project vector on y axis * float maxProjRect1Y = Vector2.Dot (vMaxRect1, axisQ); * float maxProjRect2Y = Vector2.Dot (vMaxRect2, axisQ); * float minProjRect1Y = Vector2.Dot (vMinRect1, axisQ); * float minProjRect2Y = Vector2.Dot (vMinRect2, axisQ); * * bool gapY = minProjRect2Y > maxProjRect1Y || maxProjRect2Y < minProjRect1Y; * * if (gapX || gapY) * Debug.Log("No collision"); * else * Debug.Log ("collision"); */ /* * bool collision = true; * * Vector2[] rect1Normals = GetRectangleNormal (rect1); * Vector2[] rect1AllCorners = rect1.AllCorners; * Vector2[] rect2AllCorners = rect2.AllCorners; * * //For each normal in rect1 * for (int i = 0; i < rect1Normals.Length; i++) { * * //find rect1 max min projection * 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]); * * float r1PMin = Mathf.Min (r1Dot1, Mathf.Min (r1Dot2, Mathf.Min (r1Dot3, r1Dot4))); * float r1PMax = Mathf.Max (r1Dot1, Mathf.Max (r1Dot2, Mathf.Max (r1Dot3, r1Dot4))); * * 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]); * * float r2PMin = Mathf.Min (r2Dot1, Mathf.Min (r2Dot2, Mathf.Min (r2Dot3, r2Dot4))); * float r2PMax = Mathf.Max (r2Dot1, Mathf.Max (r2Dot2, Mathf.Max (r2Dot3, r2Dot4))); * * if (r2PMin > r1PMax || r2PMax < r1PMin) { * * collision = false; * * break; * } * } * * if (collision) * Debug.Log ("Collision"); * else * Debug.Log ("No collision"); */ }
void CheckCollision() { #region old code //all corners // Vector2[] corners = rect1.AllCorners; // // bool collision = true; // // /** // * Find closest corner to circle center then make a line between that corner and circle center into projection axis // * Projecting all corner to that axis then find min and max corner // * Check if circle is overlap with rectangle // * // * This solve problem when circle contact each corner vertex // **/ // //find closest corner and compare distance // int closestCornerIndex = 0; // int currentCornerIndex = 1; // while (currentCornerIndex < corners.Length) { // // if ((circlePos - corners [closestCornerIndex]).sqrMagnitude > (circlePos - corners [currentCornerIndex]).sqrMagnitude) // closestCornerIndex = currentCornerIndex; // // currentCornerIndex++; // } // // //projection axis from closest corner to circle center // Vector2 p = circlePos - corners [closestCornerIndex]; // // //base on axis find min and max corner // float rMin = 0.0f; // float rMax = 0.0f; // for (int i = 0; i < corners.Length; i++) { // // rMax = Mathf.Max (rMax, Vector2.Dot (p.normalized, corners [i])); // rMin = Mathf.Min (rMin, Vector2.Dot (p.normalized, corners [i])); // } // // //find circle center projection // float cP = Vector2.Dot (p, circlePos); // // //check if circle is overlap rectangle // if (rMax < (cP - circleRadius) || rMin > (cP + circleRadius)) // collision = false; // else // collision = true; // // /** // * Find 4 corner's normal and make it as prjection axis // * Go throught each normal(corner) // * Find min and max projection of rectangle's corner base on that axis // * Project circle center on that axis // * Check if circle overlap rectangle // * // * This solve problem while circle contact edge of rectangle // **/ // //Ignore edge check if circle not contact with 4 corners(vertices) // if (collision == true) { // // Vector2[] normals = GetRectangleNormal (rect1); // // for (int i = 0; i < normals.Length; i++) { // // //4 corners projection // float r1Dot1 = Vector2.Dot (normals [i], corners [0]); // float r1Dot2 = Vector2.Dot (normals [i], corners [1]); // float r1Dot3 = Vector2.Dot (normals [i], corners [2]); // float r1Dot4 = Vector2.Dot (normals [i], corners [3]); // // //corner min and max on this normal(projection axis) // float r1PMin = Mathf.Min (r1Dot1, Mathf.Min (r1Dot2, Mathf.Min (r1Dot3, r1Dot4))); // float r1PMax = Mathf.Max (r1Dot1, Mathf.Max (r1Dot2, Mathf.Max (r1Dot3, r1Dot4))); // // //circle center projection on this normal(projection axis) // float circleP = Vector2.Dot (normals [i], circlePos); // // //check if circle overlap rectangle // if ((circleP - circleRadius) > r1PMax || (circleP + circleRadius) < r1PMin) { // // collision = false; // break; // } // } // } // // // if (collision) { // circleCollideDrawColor = collisionColor; // Debug.Log ("collision"); // } else { // circleCollideDrawColor = nonCollisionColor; // Debug.Log ("no collision"); // } #endregion Debug.Log(rect1.IntersectWithShape(circle)); }