Beispiel #1
0
        public static bool isColliding(CollidingRectangle r, CollidingCircle c)
        {
            //Change to int for efficiency?
            Vector2 rLoc            = new Vector2(r.absLoc.X + r.Width / 2, r.absLoc.Y + r.Height / 2);
            float   circleDistanceX = Math.Abs(c.absLoc.X - rLoc.X);
            float   circleDistanceY = Math.Abs(c.absLoc.Y - rLoc.Y);

            // float circleDistanceX = Math.Abs(c.absLoc.X - r.absLoc.X);
            // float circleDistanceY = Math.Abs(c.absLoc.Y - r.absLoc.Y);

            if (circleDistanceX > (r.Width / 2 + c.radius)) //Comparing float and int, will lose some precision
            {
                return(false);
            }
            if (circleDistanceY > (r.Height / 2 + c.radius))
            {
                return(false);
            }

            if (circleDistanceX <= (r.Width / 2))
            {
                return(true);
            }
            if (circleDistanceY <= (r.Height / 2))
            {
                return(true);
            }

            float cornerDistance_sq = (float)(Math.Pow((double)(circleDistanceX - r.Width / 2), 2) +
                                              Math.Pow(circleDistanceY - r.Height / 2, 2));

            return(cornerDistance_sq <= Math.Pow(c.radius, 2));
        }
Beispiel #2
0
 public static bool isColliding(CollidingRectangle r1, CollidingRectangle r2)
 {
     return(r1.rect.Intersects(r2.rect));
 }