/* * private static Vector3 GetPlaneNormalVector(Vector3 p1, Vector3 p2, Vector3 p3) * { * Vector3 v1 = p2 - p1; * Vector3 v2 = p3 - p1; * Vector3 normal = Vector3.Cross(v1, v2); * normal.Normalize(); * return normal; * } * * public static bool CuboidCuboidTest(Vector3[] C1Points, Vector3[] C2Points) * { * Vector3[] normalVectors = new Vector3[6]; * normalVectors[0] = GetPlaneNormalVector(C1Points[0], C1Points[3], C1Points[2]); * normalVectors[1] = GetPlaneNormalVector(C1Points[0], C1Points[4], C1Points[7]); * normalVectors[2] = GetPlaneNormalVector(C1Points[3], C1Points[2], C1Points[6]); * * normalVectors[3] = GetPlaneNormalVector(C2Points[0], C2Points[3], C2Points[2]); * normalVectors[4] = GetPlaneNormalVector(C2Points[0], C2Points[4], C2Points[7]); * normalVectors[5] = GetPlaneNormalVector(C2Points[3], C2Points[2], C2Points[6]); * * bool isIntersect = true; * for (int i = 0; i < 6; i++) * { * Vector3 normal = normalVectors[i]; * // * float c1Max = float.MinValue; * float c1Min = float.MaxValue; * foreach (var v in C1Points) * { * float projectValue = Vector3.Dot(v,normal); * if (projectValue > c1Max) * { * c1Max = projectValue; * } * if (projectValue < c1Min) * { * c1Min = projectValue; * } * } * // * float c2Max = float.MinValue; * float c2Min = float.MaxValue; * foreach (var v in C2Points) * { * float projectValue = Vector3.Dot(v, normal); * if (projectValue > c2Max) * { * c2Max = projectValue; * } * if (projectValue < c2Min) * { * c2Min = projectValue; * } * } * // * if (c2Min > c1Max || c1Min > c2Max) * { * isIntersect = false; * //Debug.Log("c1Min:" + c1Min + " c1Max" + c1Max + " c2Min:" + c2Min + " c2Max" + c2Max); * break; * } * * }//for six separate axis * return isIntersect; * } */ public static bool RectRectTest(Rect rect1, Rect rect2) { bool isIntersect = false; if (Math.Abs(rect1.position.x - rect2.position.x) < (rect1.width + rect2.width) / 2 && Math.Abs(rect2.position.y - rect1.position.y) < (rect1.height + rect2.height) / 2) { isIntersect = true; } return(isIntersect); }
public static bool RectColliderIntersectTest(Collider c1, Collider c2, out ContactInfo contactInfo) { var rc1 = c1 as RectCollider; var rc2 = c2 as RectCollider; contactInfo = null; bool intersect = !((rc1.xMin > rc2.xMax || rc2.xMin > rc1.xMax) || (rc1.yMin > rc2.yMax || rc2.yMin > rc1.yMax)); if (intersect) { Vector dir = new Vector(rc1.Position.x > rc2.Position.x ? 1 : -1, 0); Number depth = (rc1.Width + rc2.Width) / 2 - Math.Abs(rc1.Position.x - rc2.Position.x); contactInfo = new ContactInfo() { recoverDir = dir, depth = depth }; return(true); } return(intersect); }