Back to Physics in Simulations
IntermediateTeenagers
Basic Collision Detection
How do video games know when two objects hit each other? Collision detection!
The simplest form is **Axis-Aligned Bounding Box (AABB)** detection. Imagine drawing a rectangle around each of your objects that is aligned with the screen's X and Y axes.
An object (like a character or a ball) has properties:
To check if two rectangles (rect1 and rect2) are colliding, you need to check four conditions:
1. Is rect1's right side to the right of rect2's left side? (
2. Is rect1's left side to the left of rect2's right side? (
3. Is rect1's bottom side below rect2's top side? (
4. Is rect1's top side above rect2's bottom side? (
If **all four** of these conditions are true, the rectangles are overlapping, and a collision has occurred! This logic is the foundation of interaction in most 2D games.
The simplest form is **Axis-Aligned Bounding Box (AABB)** detection. Imagine drawing a rectangle around each of your objects that is aligned with the screen's X and Y axes.
An object (like a character or a ball) has properties:
x, y, width, and height.To check if two rectangles (rect1 and rect2) are colliding, you need to check four conditions:
1. Is rect1's right side to the right of rect2's left side? (
rect1.x + rect1.width > rect2.x)2. Is rect1's left side to the left of rect2's right side? (
rect1.x < rect2.x + rect2.width)3. Is rect1's bottom side below rect2's top side? (
rect1.y + rect1.height > rect2.y)4. Is rect1's top side above rect2's bottom side? (
rect1.y < rect2.y + rect2.height)If **all four** of these conditions are true, the rectangles are overlapping, and a collision has occurred! This logic is the foundation of interaction in most 2D games.
AABB Collision Function
Complete the `check_collision` function. It should return `True` if the two rectangles are colliding and `False` otherwise. Each rectangle is a dictionary with x, y, width, and height.