CHAPTER 05
Intermediate
Collision Detection Basics
Updated: May 16, 2026
25 min read
# CHAPTER 5
Collision Detection Basics
1. Introduction
If gravity pulls objects down, what stops them from falling into the abyss? Collision Detection. The ability for a computer to recognize when two digital objects occupy the exact same space is the most computationally expensive and complex part of game physics. We cannot check every polygon of a 50,000-triangle character model against the floor 60 times a second; the CPU would melt. In this chapter, we will master Collision Detection basics. We will learn how to wrap our complex models in simple, invisible mathematical shapes (Bounding Boxes and Spheres), understand how the engine detects overlap, and explore the concept of Triggers.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the necessity of simplified Collision Shapes (Colliders).
- Explain Axis-Aligned Bounding Box (AABB) collision math.
- Explain Circle/Sphere collision math using radius and distance.
- Differentiate between solid Collisions and ghost Triggers.
- Utilize Collision Layers to optimize game performance.
3. The Collider (Simplifying Reality)
Instead of checking complex 3D meshes for collisions, game developers wrap objects in invisible, primitive geometric shapes called Colliders.- Box Collider: A simple cube. Extremely fast to calculate. Used for crates, walls, and floors.
- Sphere/Circle Collider: A perfect ball. Even faster to calculate! Used for projectiles, wheels, and area-of-effect spells.
- Capsule Collider: A cylinder with rounded ends. The industry standard for humanoid players, because the rounded bottom glides smoothly up stairs and ramps.
4. Circle Collision Math (The Pythagorean Check)
How does the computer know if two circles are touching? It uses simple distance math.- 1. Find the distance between the center of Circle A and Circle B.
- 2. Add the Radius of Circle A to the Radius of Circle B.
- 3. If the Distance is LESS than the combined Radii, they are colliding!
csharp
5. Box Collision Math (AABB)
AABB stands for Axis-Aligned Bounding Box. It means a box that cannot be rotated; it is always perfectly aligned with the world's X, Y, and Z grid.-
To check if two AABBs are colliding, the CPU simply checks if the Min and Max boundaries of the boxes overlap on all three axes simultaneously. Because it's just a series of
<and>checks, it is incredibly fast.
6. Solid Collisions vs. Triggers
There are two ways the engine handles an overlapping collider:- 1. Collision (Solid): The physics engine detects the overlap, panics, and mathematically pushes the two objects apart so they don't intersect. (A car hitting a brick wall).
- 2. Trigger (Ghost): The engine detects the overlap, but does *nothing* to push them apart. It simply sends a C# message to the programmer saying, "Hey, Object A is inside Object B." (A player walking into a toxic gas cloud or collecting a coin).
7. Collision Matrix (Optimization)
Checking every object against every other object is an $O(n^2)$ problem (massive lag).- Collision Layers: You assign objects to layers (e.g., "Player", "Enemy", "Bullet").
- The Matrix: In the engine settings, you tell the physics engine: "Bullets should never check for collisions against other Bullets." This simple matrix tick-box saves immense CPU power.
8. Visual Learning: Circle Collision
txt
9. Best Practices
- Never Use Mesh Colliders for Moving Objects: A "Mesh Collider" wraps a custom 3D model perfectly. They are incredibly expensive to calculate. You can use them for static, unmoving terrain (like a complex mountain), but *never* use them for a moving car or character. Always approximate moving objects with a combination of primitive boxes and spheres.
10. Common Mistakes
- Fast-Moving Projectiles (The Bullet Through Paper Problem): A bullet is moving at 1000m/s. In Frame 1, it is in front of a thin wall. In Frame 2, it is behind the wall. The engine *never* detected an overlap, so the bullet completely ignores the wall! To fix this, high-speed objects must use Continuous Collision Detection (CCD) or Raycasts instead of standard colliders.
11. Mini Project: Raycast Hit Detection
Objective: Sometimes we don't want a physical box; we just want to shoot a laser beam to see what we hit. This is called a Raycast.
csharp
12. Practice Exercises
- 1. Why is a Capsule Collider the industry standard shape for a humanoid character?
- 2. What does AABB stand for, and what restriction is placed on this type of bounding box?
13. MCQs with Answers
Question 1
Two circles are approaching each other. Circle A has a radius of 3. Circle B has a radius of 4. The distance between their centers is currently 8. Are they colliding?
Question 2
The "Bullet Through Paper" problem occurs when a projectile moves so fast that it completely passes through a wall between Frame 1 and Frame 2 without triggering a collision. How do professional engines solve this for bullets?
14. Interview Questions
- Q: Explain the mathematical logic behind detecting a collision between two 2D Circle Colliders. Why is this significantly faster for a CPU to calculate than polygon intersection?
- Q: Contrast a physical Collision with a Trigger event. Provide a gameplay example for each.
- Q: Explain how a Collision Matrix optimizes game performance. Why is it computationally dangerous to leave all physics layers interacting with each other?