Skip to content
RED SAGSkills

Do Two Hitboxes Collide?

In a 2D game every sprite has an axis-aligned hitbox { x, y, w, h }: the top-left corner is (x, y), the box is w wide and h tall, and y grows downwards.

Return true if hitboxes a and b overlap with a positive area. Boxes that only touch along an edge or at a corner do not collide. A box with zero width or zero height never collides with anything.

Example 1

Input: a = {"x":0,"y":0,"w":10,"h":10}, b = {"x":5,"y":5,"w":10,"h":10}
Output: true
Explanation: They share the square from (5,5) to (10,10).

Example 2

Input: a = {"x":0,"y":0,"w":10,"h":10}, b = {"x":10,"y":0,"w":5,"h":5}
Output: false
Explanation: They touch along the line x = 10 only.

Constraints

  • -10^6 <= x, y <= 10^6
  • 0 <= w, h <= 10^6
  • All values are integers
Hint 1

Two boxes overlap when they overlap on the x axis and on the y axis.

Hint 2

On one axis, intervals [a1, a2) and [b1, b2) overlap when a1 < b2 && b1 < a2.

Good practice for: Game Developer, Frontend Developer, Mobile App Developer, Embedded Systems Engineer, Full Stack Developer

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.