Collision detection for beginners (with TypeScript examples) [WIP]
Published at Sep 11, 2025
In this post
Sections
Inspiration
This resource is heavily inspired by Thomas Jeffersonβs Collision Detection. I have worked with this resource on my very own project for several years now, and it helped me implement my own collision detection.
Introduction
This resource aims to explain the mathematical principles behind 2D and 3D collision detection from the basics, with code examples.
If youβre interested in seeing collision detection implementations, without all the Maths theory, click here
Prerequisites
Basic understanding of any programming language of your choice. The examples on this site will be written exclusively in TypeScript, but I will try to explain the core concepts, so you will have no trouble rewriting the examples in the language of your choice.
You will need to know how functions work and some basic data types to understand the examples in this resource. If you understand the following example, you should be good to go!
function greet(name: string){
return `Hello ${name}!`;
}HelloWorld.ts The Maths (theory) behind
Analytic Geometry
Analytic Geometry, also called coordinate geometry or Cartesian geometry, is the branch of mathematics that studies geometric figures using a coordinate system and algebraic methods. It represents points by ordered pairs (or triples) of numbers, and describes lines, curves, and shapes through equations and inequalities.
Coordinates of a point on a straight line
- The number axis on the line is determined by the choice of unit and the choice of an origin at point , which divides the line into two opposite half-lines - the positive and the negative half-axis.
Cartesian coordinate system
- in a plane (2D): consists of two mutually perpendicular number lines, called the -axis and the -axis, that intersect at a point called the origin. Every point in the plane can be uniquely represented by an ordered pair of real numbers , where is the coordinate along the -axis and is the coordinate along the -axis.
- in a space (3D): consists of three mutually perpendicular number lines, called the x-axis, y-axis, and z-axis, that intersect at a common point called the origin. Every point in space can be uniquely represented by an ordered triple of real numbers (π₯,π¦,π§), where π₯, π¦, and π§ are the coordinates along the respective axes.
Distance between two points
Along a line
The distance between two points , on a line is equal to the absolute value of the difference of real numbers and .
Try moving your cursor along this grid to interact with this example.
function calculateDistanceTwoPointsLine(x1: number, x2: number) {
distance = Math.round(Math.abs(x1 - x2));
} On a plane
The distance between two points , in the plane is determined as the size of the hypotenuse of a right triangle , where:
, ,
function calculateDistanceTwoPointsPlane(x1: number, y1: number, x2: number, y2: number) {
distance = Math.round((x2-x1)**2 + (y2-y1)**2);
} In a space
The distance between two points , in a space is determined as the size of the hypotenuse of a right triangle , using the triangle .
We use the triangle to calculate the length of the leg , needed to calculate the size of the hypotenuse of (the final distance). Make sure to take your time with the example below, in order to understand it completely.
function calculateDistanceTwoPointsSpace(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number) {
distance = Math.round((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2);
}