/// <summary> /// If an intersection with the frame occurs do the minimal translation to undo the overlap /// </summary> /// <param name="rectangleB">The AABR to check for intersect</param> public static void UndoOverlap(this AABR rectangleA, AABR rectangleB) { if (rectangleA.Intersects(rectangleB)) { Vector2[] directions = new Vector2[] { new Vector2(rectangleB.MaxX - rectangleA.X, 0), new Vector2(rectangleB.X - rectangleA.MaxX, 0), new Vector2(0, rectangleB.MaxY - rectangleA.Y), new Vector2(0, rectangleB.Y - rectangleA.MaxY) }; Vector2 minimum = directions.Aggregate((curMin, x) => (curMin == null || (x.Length) < curMin.Length) ? x : curMin); rectangleA.X += minimum.X; rectangleA.Y += minimum.Y; } }
/// <summary> /// If an intersection with the frame occurs do the minimal translation to undo the overlap /// </summary> /// <param name="rectangleB">The AABR to check for intersect</param> public static void UndoOverlap(this Box2D rectangleA, Box2D rectangleB) { if (rectangleA.Intersects(rectangleB)) { Vector2[] directions = new Vector2[] { new Vector2(rectangleB.MaxX - rectangleA.X, 0), new Vector2(rectangleB.X - rectangleA.MaxX, 0), new Vector2(0, rectangleB.MaxY - rectangleA.Y), new Vector2(0, rectangleB.Y - rectangleA.MaxY) }; Vector2 minimum = directions.Aggregate((curMin, x) => (curMin == null || (x.Length) < curMin.Length) ? x : curMin); rectangleA.X += minimum.X; rectangleA.Y += minimum.Y; } }
/// <summary> /// you whims. fine, this functions sets and positions the vertices for you. no need to worry about centering vertices about the orgin or anything. (this also sets the position of the object as the center of the vertices) /// </summary> /// <param name="verts"></param> public void ConstructFromVertices(Vector2[] verts) { if (verts.Length < 3) new Exception("verts must be >= 3."); var cent = verts.Aggregate((a, b) => a + b) / verts.Length; this.RawVertices = verts.Select(v => v - cent).ToArray(); this.SetMasterPosition(cent); }