Exemplo n.º 1
0
        static public bool Collides(Circle circle, TopLeftRectangle rectangle, out Vector2 correction)
        {
            bool result = Collides(rectangle, circle, out correction);

            correction = correction.Inverse();
            return(result);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
 static public bool Intersects(this Circle circle, TopLeftRectangle rectangle) => Collides(rectangle, circle, out _);
Exemplo n.º 10
0
 static public bool Intersects(this TopLeftRectangle rectangleA, TopLeftRectangle rectangleB)
 {
     return(Intersects(rectangleA, rectangleB, out TopLeftRectangle _));
 }
Exemplo n.º 11
0
 static public TopLeftRectangle Rescale(this TopLeftRectangle rectangle, TopLeftRectangle oldReferential, TopLeftRectangle newReferential)
 {
     return(rectangle.NormalizedCoordinates(oldReferential).Scale(newReferential));
 }
Exemplo n.º 12
0
 static public Vector2 Rescale(this Vector2 point, TopLeftRectangle oldRectangle, TopLeftRectangle newRectangle)
 {
     return(point.NormalizedCoordinates(oldRectangle).Scale(newRectangle));
 }
Exemplo n.º 13
0
 static public TopLeftRectangle Scale(this TopLeftRectangle rectangle, TopLeftRectangle referential)
 {
     return(new TopLeftRectangle(rectangle.Position * referential.Size + referential.Position, rectangle.Size * referential.Size));
 }
Exemplo n.º 14
0
 static public Vector2 Scale(this Vector2 point, TopLeftRectangle rectangle)
 {
     return(point * rectangle.Size + rectangle.Position);
 }
Exemplo n.º 15
0
 static public TopLeftRectangle NormalizedCoordinates(this TopLeftRectangle rectangle, TopLeftRectangle referential)
 {
     return(new TopLeftRectangle((rectangle.Position - referential.Position) / referential.Size, rectangle.Size / referential.Size));
 }