Exemplo n.º 1
0
        /// <summary>
        /// Checks for a collision between a bounding circle and an axis-aligned bounding box.
        /// </summary>
        /// <param name="circle">Bounding circle.</param>
        /// <param name="box">Axis-aligned bounding box.</param>
        /// <returns>True if a collision was found.</returns>
        public static bool CollisionCircleAABox(BoundingCircle circle, BoundingBoxAA box)
        {
            // Find the point within the box that is closest to the circle center
            Vector2 closestPoint = box.Min + (circle.Center - box.Min);

            closestPoint.X = MathHelper.Clamp(closestPoint.X, box.Min.X, box.Max.X);
            closestPoint.Y = MathHelper.Clamp(closestPoint.Y, box.Min.Y, box.Max.Y);

            // Check if the point closest to the circle center is within the circle
            return((closestPoint - circle.Center).Length() < circle.Radius);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks for a collisions between two axis-aligned bounding boxes.
        /// </summary>
        /// <param name="box1">First bounding box.</param>
        /// <param name="box2">Second bounding box.</param>
        /// <returns>True if a collision was found.</returns>
        public static bool CollisionAABoxAABox(BoundingBoxAA box1, BoundingBoxAA box2)
        {
            // Seperating Axis Theorem is used for determining collisions
            if (box1.Max.X < box2.Min.X || box1.Min.X > box2.Max.X)
            {
                return(false);
            }
            if (box1.Max.Y < box2.Min.Y || box1.Min.Y > box2.Max.Y)
            {
                return(false);
            }

            return(true);
        }