Collision detection between two rectangles in java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Axis-Aligned Bounding Box (AABB) collision detection checks whether two rectangles overlap by comparing their edges. Two rectangles collide if and only if they overlap on both the x-axis and the y-axis. This runs in O(1) time and is the foundation of 2D collision detection in games and UI frameworks.
The AABB Overlap Condition
Two rectangles A and B overlap when all four of these conditions are true:
- A's left edge is to the left of B's right edge
- A's right edge is to the right of B's left edge
- A's top edge is above B's bottom edge
- A's bottom edge is below B's top edge
In code (where y increases downward, as in most screen coordinate systems):
If any one condition is false, the rectangles do not overlap.
Basic Implementation
Using java.awt.Rectangle
Java's standard library provides java.awt.Rectangle with built-in collision detection:
Using Rectangle2D for Floating-Point Precision
For sub-pixel precision, use Rectangle2D.Double:
Game Loop Example
A typical game loop with collision detection:
Optimization: Broad Phase with Spatial Partitioning
When checking many objects, testing every pair is O(n²). Use spatial partitioning to reduce checks:
Common Pitfalls
- Coordinate system: Screen coordinates typically have y increasing downward. Mathematical coordinates have y increasing upward. Make sure your collision conditions match your coordinate system.
- Edge-touching: The conditions above use strict inequality (
<,>), so rectangles that touch edges without overlapping are not considered colliding. Use<=if you want edge-touching to count. - Integer overflow:
java.awt.Rectangleusesintfields. Very large coordinates or sizes can overflow. UseRectangle2D.Doublefor large worlds. - Moving objects: Fast-moving objects can pass through thin obstacles between frames (tunneling). For high-speed objects, use swept collision detection or reduce the time step.
- Rotated rectangles: AABB only works for axis-aligned rectangles. For rotated rectangles, use the Separating Axis Theorem (SAT) instead.
Summary
- Two axis-aligned rectangles collide when they overlap on both axes — four simple comparisons
- Use
java.awt.Rectangle.intersects()for built-in support or implement your own for custom types - Calculate minimum translation vector to resolve collisions by pushing back on the axis with less overlap
- Use spatial partitioning (grids, quadtrees) when checking many objects to avoid O(n²) pair checks
- AABB does not handle rotated rectangles — use SAT for that case
Related reading
- Combining multiple SuppressWarnings annotations - Eclipse Indigo
- Combining Overlapping Date Ranges - Java
- Combining Unknown number of Observables in RxJava
- com.jcraft.jsch.JSchException UnknownHostKey
- Command line progress bar in Java
- Common algorithm for generating a diff of the fields in two beans?
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException Communications link failure
- Compare every item to every other item in ArrayList

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.