Skip to main content
Game Physics – Complete Beginner to Advanced Guide
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. 1. Find the distance between the center of Circle A and Circle B.
  1. 2. Add the Radius of Circle A to the Radius of Circle B.
  1. 3. If the Distance is LESS than the combined Radii, they are colliding!
csharp
1234567
// Conceptual Math for Circle Collision
float distance = Vector3.Distance(circleA.position, circleB.position);
float combinedRadii = circleA.radius + circleB.radius;

if (distance < combinedRadii) {
    Console.WriteLine("CRASH!");
}

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. 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).
  1. 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
123456
   (Circle A)                 (Circle B)
    Radius: 2                  Radius: 3
       .-------------(5m)------------.
       
Distance (5) is EXACTLY EQUAL TO combined Radii (2+3).
They are perfectly touching! If distance drops to 4.9, they overlap!

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
1234567891011121314151617181920
class SniperRifle
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Shoot an invisible mathematical laser forward
            RaycastHit hitInfo;
            
            // Physics.Raycast(StartPos, Direction, OutputData, MaxDistance)
            if (Physics.Raycast(transform.position, transform.forward, out hitInfo, 1000f))
            {
                Console.WriteLine("The laser hit: " + hitInfo.collider.name);
                
                // Spawn a bullet hole exactly where the laser hit the wall!
                Instantiate(bulletHoleDecal, hitInfo.point, Quaternion.identity);
            }
        }
    }
}

12. Practice Exercises

  1. 1. Why is a Capsule Collider the industry standard shape for a humanoid character?
  1. 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?

15. FAQs

Q: Can I combine shapes? A: Yes! This is called a "Compound Collider." You can attach a Box Collider for a character's torso, a Sphere for their head, and capsules for their limbs. The engine treats them as one single, highly accurate physical object.

16. Summary

In Chapter 5, we gave our objects substance. We learned that video games use simplified mathematical primitives—Boxes, Spheres, and Capsules—to efficiently detect overlaps. We explored the lightning-fast math of Circle distances and AABB overlap checks. We distinguished between hard, physical collisions (walls) and ghost triggers (gas clouds). Finally, we learned how to optimize our CPU load using Collision Matrices and bypass fast-moving projectile glitches using Raycasts.

17. Next Chapter Recommendation

Our objects have mass, and they can collide. It is time to let the engine take control of the chaos. Proceed to Chapter 6: Rigidbody Physics Systems.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·