← Blog

[11 Sep 2025 · work in progress]

Collision detection for beginners (with code examples)

A short book on collision detection, built on analytic geometry. Each chapter turns one idea into a collision test, with demos and code in TypeScript, JavaScript and Python.

Inspiration

This resource is heavily inspired by Jeff Thompson’s Collision Detection. I have worked with it on my own projects for several years now, and it helped me implement my own collision detection: every 2D algorithm in this book has been used in Hallify, my venue booking platform.

Introduction

Collision detection answers one question: do two shapes share at least one point? A program can’t answer that by looking. To a program, a ball and a wall are just numbers. Analytic geometry is the part of maths that turns shapes into numbers and equations, so the question becomes arithmetic that takes a computer a handful of operations.

This is a small book. Each chapter builds on the one before it: coordinates first, then distances, then shapes as equations, and every idea ends in a collision test with a demo you can play with. Part I works in the plane (2D), and Part II takes the same ideas into space (3D).

If you’re only after the code, jump to all the functions of Part I.

Prerequisites

Basic understanding of any programming language of your choice. Every example comes in TypeScript, JavaScript and Python: pick one with the tabs above any code block, and the whole book switches with it. I explain the core concepts as well, so you’ll have no trouble rewriting the examples in any other language. You will need to know how functions work and some basic data types. If you understand the following example, you’re good to go!

HelloWorld.ts
function greet(name: string): string {
	return `Hello ${name}!`;
}
HelloWorld.js
function greet(name) {
	return `Hello ${name}!`;
}
hello_world.py
def greet(name: str) -> str:
    return f"Hello {name}!"

Time and space complexity

Every function in this book comes with its time and space complexity, in big-O notation. Time complexity says how the number of steps grows as the input grows, and space complexity how much extra memory a function needs on top of its input.

O(1)O(1) means it doesn’t grow at all: comparing two circles takes the same handful of steps however big they are, or however far apart. The polygon chapters count vertices: with nn of them, O(n)O(n) grows in step with nn, and O(n2)O(n^2) with its square, so twice the vertices means four times the work.

Contents

Part I · 2D

  1. 01 Analytic geometry How a coordinate system turns points and shapes into numbers, which is all a program can work with.
  2. 02 Distance between two points The distance formula on a line and in the plane, straight from Pythagoras' theorem, and the trick that lets collision tests skip the square root.
  3. 03 Midpoint of a line segment The point exactly halfway between two others, which is just the average of their coordinates.
  4. 04 Circles The equation of a circle, and the two collision tests that fall out of it, point vs circle and circle vs circle.
  5. 05 Rectangles Rectangles as a pair of intervals, the overlap test that makes rectangle vs rectangle cheap, and clamping for circle vs rectangle.
  6. 06 Lines The general equation of a line, which side of a line a point is on, the distance from a point to a line, and circle vs line.
  7. 07 Line segments Vectors and the dot product, projecting a point onto a segment, circle vs segment, line vs segment and segment vs segment, the tests behind walls, platforms and polygon edges.
  8. 08 Convex polygons Convex polygons against a point, a line, another polygon, a rectangle, a segment and a circle, with the separating axis theorem at the centre.
  9. 09 Concave polygons Why the separating axis theorem fails on polygons with dents, edge-based tests that work on any polygon, and the ups and downs of both approaches.
  10. 10 All the functions Every function from Part I in one file, ready to copy, in TypeScript, JavaScript or Python.

Part II · 3D

  1. 11 Coordinates and distance in space Three axes instead of two, which way game engines point them, vectors in space, and the distance and midpoint of two points in 3D.
  2. 12 Spheres The equation of a sphere, and the two collision tests that come with it, point vs sphere and sphere vs sphere.
  3. 13 Boxes Axis-aligned boxes as three intervals, and the tests that follow, point vs box, box vs box and sphere vs box.
  4. 14 Planes The cross product, the equation of a plane, which side of a plane a point is on and how far away it is, and sphere vs plane and box vs plane.
  5. 15 Rays and segments Lines in space written with a parameter, the closest point of a segment, sphere vs segment and capsules, segment vs plane, and rays against planes, spheres and boxes.
  6. 16 Triangles Why everything in 3D is made of triangles, their normal and area, which way they face, and a point, a ray and a sphere against a triangle.
  7. 17 Polygons into triangles Splitting a flat polygon in space into triangles, convex or concave. Its normal, a fan, ear clipping, flattening to 2D, holes, and testing any polygon or mesh through its triangles.
  8. 18 The separating axis test in 3D The separating axis test for convex shapes in space. Face normals, the edge × edge axes that 3D adds, turned boxes, and triangle vs box.
  9. 19 All the functions in 3D Every function from Part II in one file, ready to copy, in TypeScript, JavaScript or Python.
Start reading: Analytic geometry →

Comments

No comments yet. Questions and corrections are welcome.

Plain text, line breaks kept. Your IP address is stored only as a one-way hash, to limit spam.