public static bool Overlaps(Sprite s1, Sprite s2) { if (s1.Position.X + s1.Size.Width < s2.Position.X) return false; if (s1.Position.X > s2.Position.X + s2.Size.Width) return false; if (s1.Position.Y + s1.Size.Height < s2.Position.Y) return false; if (s1.Position.Y > s2.Position.Y + s2.Size.Height) return false; return true; }
/// <summary> /// Calculates if rectangles describing two sprites /// are overlapping on screen. /// </summary> /// <param name="s1">First sprite</param> /// <param name="s2">Second sprite</param> /// <returns>Returns true if overlapping</returns> public static bool Overlaps(Sprite s1, Sprite s2) { if (s1.Position.X > s2.Position.X && s1.Position.X < s2.Position.X + s2.Size.Width && s1.Position.Y > s2.Position.Y && s1.Position.Y < s2.Position.Y + s2.Size.Height || s1.Position.X > s2.Position.X && s1.Position.X < s2.Position.X + s2.Size.Width && s1.Position.Y + s1.Size.Height > s2.Position.Y && s1.Position.Y + s1.Size.Height < s2.Position.Y + s2.Size.Height || s1.Position.X + s1.Size.Width > s2.Position.X && s1.Position.X + s1.Size.Width < s2.Position.X + s2.Size.Width && s1.Position.Y > s2.Position.Y && s1.Position.Y < s2.Position.Y + s2.Size.Height || s1.Position.X + s1.Size.Width > s2.Position.X && s1.Position.X + s1.Size.Width < s2.Position.X + s2.Size.Width && s1.Position.Y + s1.Size.Height > s2.Position.Y && s1.Position.Y + s1.Size.Height < s2.Position.Y + s2.Size.Height) return true; else return false; }
/// <summary> /// Calculates if rectangles describing two sprites /// are overlapping on screen. /// </summary> /// <param name="s1">First sprite</param> /// <param name="s2">Second sprite</param> /// <returns>Returns true if overlapping</returns> public static bool Overlaps(Sprite s1, Sprite s2) { float s1x = s1.Position.X; float s1y = s1.Position.Y; float s1w = s1.Size.Width; float s1h = s1.Size.Height; float s2x = s2.Position.X; float s2y = s2.Position.Y; float s2w = s2.Size.Width; float s2h = s2.Size.Height; // overlapping on x if ((s1x + s1w > s2x && s1x < s2x + s2w) //overlapping on y && (s1y + s1h > s2y && s1y < s2y + s2h)) { return true; } return false; }