private static float2 Depenetrate(RectangleCollider rectangleA, RectangleCollider rectangleB) { float2 depenetration = float2.zero; float depth = float.PositiveInfinity; for (int i = 0; rectangleA.axes.Count > i; i++) { float2 axis = math.normalizesafe(rectangleA.axes[i]); float overlap = LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)); if (overlap == 0) { return(float2.zero); } if (depth > overlap) { depth = overlap; depenetration = axis; } } for (int i = 0; rectangleB.axes.Count > i; i++) { float2 axis = math.normalizesafe(rectangleB.axes[i]); float overlap = LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)); if (overlap == 0) { return(float2.zero); } if (depth > overlap) { depth = overlap; depenetration = axis; } } return(depenetration * depth * (math.dot(depenetration, rectangleB.position - rectangleA.position) >= 0 ? -1 : 1)); }
private static bool Intersects(RectangleCollider rectangle, PolygonCollider polygon) { for (int i = 0; rectangle.axes.Count > i; i++) { float2 axis = rectangle.axes[i]; if (LineOverlap(Project(rectangle, axis), Project(polygon, axis)) == 0) { return(false); } } for (int i = 0; polygon.points.Count > i; i++) { float2 axis = polygon.edges[i].perpendicular(); if (LineOverlap(Project(rectangle, axis), Project(polygon, axis)) == 0) { return(false); } } return(true); }
private static bool Intersects(RectangleCollider rectangleA, RectangleCollider rectangleB) { for (int i = 0; rectangleA.axes.Count > i; i++) { float2 axis = rectangleA.axes[i]; if (LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)) == 0) { return(false); } } for (int i = 0; rectangleB.axes.Count > i; i++) { float2 axis = rectangleB.axes[i]; if (LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)) == 0) { return(false); } } return(true); }
private static bool ContainedBy(float2 point, RectangleCollider rectangleCollider) => rectangleCollider.radians != 0 ? ContainedBy(point, rectangleCollider as ShapeCollider) : rectangleCollider.xMax > point.x && point.x > rectangleCollider.xMin && rectangleCollider.yMax > point.y && point.y > rectangleCollider.yMin;
private static bool Intersects(PolygonCollider polygon, RectangleCollider rectangle) => Intersects(rectangle, polygon);