static public bool Collides(Circle circle, TopLeftRectangle rectangle, out Vector2 correction) { bool result = Collides(rectangle, circle, out correction); correction = correction.Inverse(); return(result); }
static public Vector2 NormalizedCoordinates(this Vector2 point, TopLeftRectangle referential) { Vector2 result = (point - referential.Position) / referential.Size; if (float.IsNaN(result.X)) { result = result.SetX(0); } if (float.IsNaN(result.Y)) { result = result.SetY(0); } return(result); }
static public bool Intersects(this TopLeftRectangle rectangleA, TopLeftRectangle rectangleB, out TopLeftRectangle intersection) { float left = MathHelper.Max(rectangleA.Left, rectangleB.Left); float right = MathHelper.Min(rectangleA.Right, rectangleB.Right); float top = MathHelper.Max(rectangleA.Top, rectangleB.Top); float bottom = MathHelper.Min(rectangleA.Bottom, rectangleB.Bottom); if (left >= right || top >= bottom) { intersection = TopLeftRectangle.Void; return(false); } intersection = new TopLeftRectangle(left, top, right - left, bottom - top); return(true); }
static public bool Collides(TopLeftRectangle rectangle, TopLeftRectangle other, out Vector2 correction) { if (Intersects(rectangle, other, out TopLeftRectangle intersection)) { bool isWiderThanTall = intersection.Width > intersection.Height; if (isWiderThanTall) { correction = rectangle.Center.Y <= other.Center.Y ? new Vector2(0, -intersection.Height) : new Vector2(0, intersection.Height); } else { correction = rectangle.Center.X <= other.Center.X ? new Vector2(-intersection.Width, 0) : new Vector2(intersection.Width, 0); } return(true); } correction = Vector2.Zero; return(false); }
static public Vector2 ClampToRectangle(this Vector2 point, TopLeftRectangle rectangle) { if (point.X < rectangle.Left) { point.X = rectangle.Left; } if (point.X > rectangle.Right) { point.X = rectangle.Right; } if (point.Y < rectangle.Top) { point.Y = rectangle.Top; } if (point.Y > rectangle.Bottom) { point.Y = rectangle.Bottom; } return(point); }
static public bool Collides(TopLeftRectangle rectangle, Circle circle, out Vector2 correction) { // Find the closest point to the circle within the rectangle float closestX = MathHelper.Clamp(circle.Center.X, rectangle.Left, rectangle.Right); float closestY = MathHelper.Clamp(circle.Center.Y, rectangle.Top, rectangle.Bottom); var closest = new Vector2(closestX, closestY); // Calculate the distance between the circle's center and this closest point Vector2 distance = circle.Center - closest; // If the distance is less than the circle's radius, an intersection occurs float radiusIntersection = circle.Radius - distance.Length(); if (radiusIntersection > 0) { correction = radiusIntersection * (closest - rectangle.Center).Normalized(); return(true); } correction = Vector2.Zero; return(false); }
static public TopLeftRectangle EncaseRectangle(this TopLeftRectangle inner, TopLeftRectangle outer) { if (inner.Left < outer.Left) { inner.Left += outer.Left - inner.Left; } else if (inner.Right > outer.Right) { inner.Left -= inner.Right - outer.Right; } if (inner.Top < outer.Top) { inner.Top += outer.Top - inner.Top; } else if (inner.Bottom > outer.Bottom) { inner.Top -= inner.Bottom - outer.Bottom; } return(inner); }
static public TopLeftRectangle ClampToRectangle(this TopLeftRectangle inner, TopLeftRectangle outer) { if (inner.Left < outer.Left) { inner.Width -= outer.Left - inner.Left; inner.Left = outer.Left; } if (inner.Right > outer.Right) { inner.Width -= inner.Right - outer.Right; } if (inner.Top < outer.Top) { inner.Height -= outer.Top - inner.Top; inner.Top = outer.Top; } if (inner.Bottom > outer.Bottom) { inner.Height -= inner.Bottom - outer.Bottom; } return(inner); }
static public bool Intersects(this Circle circle, TopLeftRectangle rectangle) => Collides(rectangle, circle, out _);
static public bool Intersects(this TopLeftRectangle rectangleA, TopLeftRectangle rectangleB) { return(Intersects(rectangleA, rectangleB, out TopLeftRectangle _)); }
static public TopLeftRectangle Rescale(this TopLeftRectangle rectangle, TopLeftRectangle oldReferential, TopLeftRectangle newReferential) { return(rectangle.NormalizedCoordinates(oldReferential).Scale(newReferential)); }
static public Vector2 Rescale(this Vector2 point, TopLeftRectangle oldRectangle, TopLeftRectangle newRectangle) { return(point.NormalizedCoordinates(oldRectangle).Scale(newRectangle)); }
static public TopLeftRectangle Scale(this TopLeftRectangle rectangle, TopLeftRectangle referential) { return(new TopLeftRectangle(rectangle.Position * referential.Size + referential.Position, rectangle.Size * referential.Size)); }
static public Vector2 Scale(this Vector2 point, TopLeftRectangle rectangle) { return(point * rectangle.Size + rectangle.Position); }
static public TopLeftRectangle NormalizedCoordinates(this TopLeftRectangle rectangle, TopLeftRectangle referential) { return(new TopLeftRectangle((rectangle.Position - referential.Position) / referential.Size, rectangle.Size / referential.Size)); }