← 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). Part II takes the same ideas into space (3D), once Part I is finished.

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

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 follows once the 2D part is finished.

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.