0 hits

Collision Detection

How do games know when things touch?

1

The Fundamental Question

Every frame, games ask thousands of times: "Are these two things overlapping?" Drag the shapes to see collision in action. When they overlap, they turn green!

No Collision
Status
0
Checks This Session

⚡ Performance Matters

Games can have 100+ objects. Checking every pair means 5,000+ collision tests per frame!

📈 60 FPS = 60 Checks/Second

Each collision must be detected within 16ms. That's why simple math is crucial.

2

AABB: Axis-Aligned Bounding Boxes

The simplest collision check! If two boxes don't overlap on ANY axis, they can't be colliding. We just need 4 comparisons. Drag the boxes to test!

No Collision
Status
No
X Overlap?
No
Y Overlap?
A.left < B.right && A.right > B.left && A.top < B.bottom && A.bottom > B.top
If ALL conditions are true = collision!
Why "Axis-Aligned"?

These boxes can't rotate - their edges are always parallel to the X and Y axes. This makes the math super fast! For rotated boxes, you need more complex algorithms.

3

Circle Collision: Distance Check

Circles are even simpler! If the distance between centers is less than the sum of radii, they're colliding. Drag the circles to see the math in action!

150
Distance
100
Sum of Radii
No Collision
Status
distance = √((x₂-x₁)² + (y₂-y₁)²)
If distance < radius₁ + radius₂ = collision!

🚀 Optimization Trick

Square roots are slow! Games often compare distance² vs (r₁+r₂)² instead. Same result, faster math!

🎮 Perfect For

Balls, coins, bullets, explosions, character hitboxes - anything roughly circular!

4

Box vs Circle: The Tricky Case

What if you need to check a circle against a box? Find the closest point on the box to the circle's center, then check if that point is within the circle's radius!

(200, 150)
Closest Point
50
Distance to Center
No Collision
Status

🤔 The Algorithm

1. Clamp circle center X to box's left/right edges
2. Clamp circle center Y to box's top/bottom edges
3. That's your closest point!
4. Check if distance from closest point to circle center < radius

5

Why Simple Shapes?

Game characters have complex outlines, but collision uses simple shapes. Why? Performance! Watch how many collision checks happen in real-time.

5
Objects
10
Checks/Frame
0
Active Collisions

◻ AABB Check

4

comparisons

◯ Circle Check

5

operations

⬢ Pixel Perfect

1000s

per object pair!

6

Compound Colliders

A single box can't fit a character's shape! Games use multiple simple colliders combined. This character has separate hitboxes for head, body, and limbs - each can detect hits independently!

Drag launcher to aim | Click to select | Drag to move/resize | +Add for new shapes

6
Colliders
0
Total Hits
None
Selected
💡 Why Multiple Colliders?
Head Shot

Extra damage in shooters!

Body Types

Different armor zones

Limb Damage

Disable arms or legs

Real Game Example

Hollow Knight's main character uses about 30 different collision shapes! Different hitboxes for attacking, getting hit, and touching the environment.

7

Point in Polygon: Click Detection

How does a game know if you clicked ON a button or character? The "ray casting" algorithm! Cast a ray from the point - if it crosses an odd number of edges, you're inside. Click anywhere!

0
Edge Crossings
Outside
Result
Odd crossings = Inside | Even crossings = Outside
The Ray Casting Algorithm
8

Spatial Partitioning: Be Smart!

With 100 objects, checking every pair = 4,950 checks! But objects far apart can't collide. Spatial partitioning divides space into cells - only check objects in the same cell!

20
Objects
190
Naive Checks
~40
With Grid
79%
Checks Saved

🎮 Quadtrees

Recursively divide space into 4 quadrants. Great for varying object density!

🛠 Uniform Grid

Simple fixed grid. Fast and easy to implement. Used in many 2D games.

9

Collision Playground

Put it all together! Control the player with arrow keys or WASD. Collect coins, avoid enemies, and see collision detection in action!

Click canvas, then: Arrow keys/WASD = move

0
Coins
0
Collision Checks
0
Enemy Hits

Collision Master!

You've learned how games detect when objects touch! From simple boxes to complex compound colliders, you now understand the math behind every jump, hit, and pickup.

0
Collision Checks
0
Coins Collected
0
Time Exploring

AABB

Axis-aligned boxes: 4 simple comparisons

Circle Collision

Distance vs sum of radii

Box vs Circle

Find closest point, check distance

Compound Colliders

Complex shapes from simple ones

Point in Polygon

Ray casting for click detection

Spatial Partitioning

Optimize by dividing space

Ready to Create?

Put your new knowledge into practice!

More Discoveries

Suggest a Correction