Reference Updated June 2026

Mathematical Formulae

Vectors, matrices, quaternions, ODE integration methods, and key physics constants — every formula you need when building or studying 3D browser simulations.

📐 Vector Operations

Vectors are the building blocks of every simulation. All formulas below apply to $\mathbb{R}^3$ unless noted; the 2D equivalents drop the $z$ component.

Basic operations

Operation Formula Three.js / JS
Addition $\mathbf{a} + \mathbf{b} = (a_x+b_x,\; a_y+b_y,\; a_z+b_z)$ a.add(b)
Scalar multiply $s\,\mathbf{a} = (s\,a_x,\; s\,a_y,\; s\,a_z)$ a.multiplyScalar(s)
Magnitude $|\mathbf{a}| = \sqrt{a_x^2 + a_y^2 + a_z^2}$ a.length()
Normalise $\hat{\mathbf{a}} = \dfrac{\mathbf{a}}{|\mathbf{a}|}$ a.normalize()
Dot product $\mathbf{a} \cdot \mathbf{b} = a_x b_x + a_y b_y + a_z b_z$ a.dot(b)
Cross product $\mathbf{a} \times \mathbf{b} = (a_y b_z - a_z b_y,\; a_z b_x - a_x b_z,\; a_x b_y - a_y b_x)$ a.cross(b)
Angle between $\theta = \arccos\!\left(\dfrac{\mathbf{a} \cdot \mathbf{b}}{|\mathbf{a}||\mathbf{b}|}\right)$ a.angleTo(b)
Linear interpolation $\mathbf{c} = \mathbf{a} + t(\mathbf{b} - \mathbf{a}),\quad t\in[0,1]$ a.lerp(b, t)
Reflect $\mathbf{r} = \mathbf{d} - 2(\mathbf{d}\cdot\hat{\mathbf{n}})\hat{\mathbf{n}}$ d.reflect(n)

Dot product identities

Geometric interpretation $$\mathbf{a} \cdot \mathbf{b} = |\mathbf{a}||\mathbf{b}|\cos\theta$$ $$\mathbf{a} \cdot \mathbf{a} = |\mathbf{a}|^2 \quad\text{(squared length)}$$ $$\mathbf{a} \perp \mathbf{b} \;\Longleftrightarrow\; \mathbf{a}\cdot\mathbf{b} = 0$$

🔢 Matrix Transforms

Three.js stores matrices in column-major order in a flat Float32Array. All transforms below produce 4×4 homogeneous matrices.

Translation

T(tx, ty, tz) $$T = \begin{pmatrix}1&0&0&t_x\\0&1&0&t_y\\0&0&1&t_z\\0&0&0&1\end{pmatrix}$$

Scale

S(sx, sy, sz) $$S = \begin{pmatrix}s_x&0&0&0\\0&s_y&0&0\\0&0&s_z&0\\0&0&0&1\end{pmatrix}$$

Rotation about the Y axis

Ry(θ) $$R_y(\theta) = \begin{pmatrix}\cos\theta&0&\sin\theta&0\\0&1&0&0\\-\sin\theta&0&\cos\theta&0\\0&0&0&1\end{pmatrix}$$

Model-View-Projection (MVP)

Vertex transform pipeline $$\mathbf{v}_{clip} = M_{proj} \cdot M_{view} \cdot M_{model} \cdot \mathbf{v}_{local}$$
Matrix Three.js property Purpose
Model (M) object.matrixWorld Object → world space (position + rotation + scale)
View (V) camera.matrixWorldInverse World → camera space
Projection (P) camera.projectionMatrix Camera → clip space (perspective divide)
Normal matrix object.normalMatrix Transforms normals (inverse-transpose of upper-left 3×3 of MV)

🌀 Quaternions

Quaternions represent 3D rotations without gimbal lock. A unit quaternion $\mathbf{q} = (w, x, y, z)$ with $|\mathbf{q}|=1$ encodes a rotation of $2\arccos(w)$ around axis $(x,y,z)/\sin(\arccos w)$.

Rotation quaternion from axis-angle $$\mathbf{q} = \left(\cos\frac{\theta}{2},\; \hat{\mathbf{n}}\sin\frac{\theta}{2}\right)$$
Quaternion multiplication (composition of rotations) $$\mathbf{q}_1 \otimes \mathbf{q}_2 = \begin{pmatrix} w_1 w_2 - x_1 x_2 - y_1 y_2 - z_1 z_2 \\ w_1 x_2 + x_1 w_2 + y_1 z_2 - z_1 y_2 \\ w_1 y_2 - x_1 z_2 + y_1 w_2 + z_1 x_2 \\ w_1 z_2 + x_1 y_2 - y_1 x_2 + z_1 w_2 \end{pmatrix}$$
Rotate vector v by quaternion q $$\mathbf{v}' = \mathbf{q} \otimes (0, \mathbf{v}) \otimes \mathbf{q}^{-1}$$
SLERP — Spherical Linear Interpolation $$\text{slerp}(\mathbf{q}_0, \mathbf{q}_1, t) = \mathbf{q}_0 \left(\mathbf{q}_0^{-1}\mathbf{q}_1\right)^t, \quad t\in[0,1]$$
Operation Three.js
From axis-angle q.setFromAxisAngle(axis, angle)
From Euler q.setFromEuler(euler)
Multiply (compose) q.multiply(r)
Slerp q.slerp(target, t)
Conjugate / inverse q.conjugate() (assuming unit)
Apply to vector vec.applyQuaternion(q)

⏱ ODE Integration Methods

For a system $\dot{\mathbf{x}} = f(\mathbf{x}, t)$ where $\mathbf{x}$ is the state vector (positions + velocities), these methods advance the state by one timestep $\Delta t$.

Euler (explicit) — 1st order

O(Δt) global error — simplest, least stable $$\mathbf{x}_{n+1} = \mathbf{x}_n + \Delta t \cdot f(\mathbf{x}_n, t_n)$$

Symplectic Euler (Semi-implicit Euler) — 1st order

Energy-preserving for Hamiltonian systems — preferred for gravity/springs $$\mathbf{v}_{n+1} = \mathbf{v}_n + \Delta t \cdot \mathbf{a}_n$$ $$\mathbf{x}_{n+1} = \mathbf{x}_n + \Delta t \cdot \mathbf{v}_{n+1}$$

Verlet — 2nd order

O(Δt²) position error · good for molecular dynamics $$\mathbf{x}_{n+1} = 2\mathbf{x}_n - \mathbf{x}_{n-1} + \Delta t^2\,\mathbf{a}_n$$

Velocity Verlet (Leapfrog) — 2nd order

Recommended default for particle physics $$\mathbf{v}_{n+\tfrac12} = \mathbf{v}_n + \tfrac{\Delta t}{2}\,\mathbf{a}_n$$ $$\mathbf{x}_{n+1} = \mathbf{x}_n + \Delta t\,\mathbf{v}_{n+\tfrac12}$$ $$\mathbf{v}_{n+1} = \mathbf{v}_{n+\tfrac12} + \tfrac{\Delta t}{2}\,\mathbf{a}_{n+1}$$

Runge-Kutta 4 (RK4) — 4th order

O(Δt⁴) error · expensive but very accurate $$k_1 = f(\mathbf{x}_n,\; t_n)$$ $$k_2 = f\!\left(\mathbf{x}_n + \tfrac{\Delta t}{2}k_1,\; t_n + \tfrac{\Delta t}{2}\right)$$ $$k_3 = f\!\left(\mathbf{x}_n + \tfrac{\Delta t}{2}k_2,\; t_n + \tfrac{\Delta t}{2}\right)$$ $$k_4 = f\!\left(\mathbf{x}_n + \Delta t\,k_3,\; t_n + \Delta t\right)$$ $$\mathbf{x}_{n+1} = \mathbf{x}_n + \frac{\Delta t}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$
Method Order Evals/step Best for
Euler 1 1 Quick prototypes, non-critical
Symplectic Euler 1 1 Springs, gravity, particle sims
Verlet 2 1 Molecular dynamics, cloth
Velocity Verlet 2 2 N-body, SPH, general physics
RK4 4 4 Lorenz attractor, pendulum, ODEs

🚀 Kinematics & Dynamics

Newtonian mechanics

Law Formula
Newton's second law $\mathbf{F} = m\,\mathbf{a}$
Universal gravitation $F = G\dfrac{m_1 m_2}{r^2}$,   $\mathbf{F}_{12} = G\dfrac{m_1 m_2}{r^3}\,\mathbf{r}_{12}$
Kinetic energy $K = \tfrac{1}{2}m\,v^2$
Gravitational PE $U = -G\dfrac{m_1 m_2}{r}$
Elastic spring force $F = -k(x - x_0)$ (Hooke's law)
Damping force $F_d = -b\,v$ (linear damping)
Drag force $F_D = \tfrac{1}{2}\rho\,C_D\,A\,v^2$
Momentum $\mathbf{p} = m\mathbf{v}$,   conserved in closed systems
Angular momentum $\mathbf{L} = \mathbf{r} \times \mathbf{p} = I\,\boldsymbol{\omega}$
Torque $\boldsymbol{\tau} = \mathbf{r} \times \mathbf{F} = I\,\boldsymbol{\alpha}$

Elastic collision (1D)

Post-collision velocities for masses m₁, m₂ $$v_1' = \frac{m_1 - m_2}{m_1 + m_2}v_1 + \frac{2m_2}{m_1+m_2}v_2$$ $$v_2' = \frac{2m_1}{m_1 + m_2}v_1 + \frac{m_2 - m_1}{m_1+m_2}v_2$$

SPH pressure force kernel (Spiky)

Desbrun 1996 — standard SPH pressure gradient $$\nabla W_{spiky}(r, h) = -\frac{45}{\pi h^6}(h-r)^2\,\hat{\mathbf{r}}, \quad r \le h$$

⚛ Physical Constants

G
Gravitational constant
6.674 × 10⁻¹¹ N m² kg⁻²
c
Speed of light
2.998 × 10⁸ m s⁻¹
h
Planck constant
6.626 × 10⁻³⁴ J s
kB
Boltzmann constant
1.381 × 10⁻²³ J K⁻¹
NA
Avogadro constant
6.022 × 10²³ mol⁻¹
e
Elementary charge
1.602 × 10⁻¹⁹ C
me
Electron mass
9.109 × 10⁻³¹ kg
ε0
Vacuum permittivity
8.854 × 10⁻¹² F m⁻¹
g
Standard gravity (Earth)
9.80665 m s⁻²
R
Ideal gas constant
8.314 J mol⁻¹ K⁻¹
Simulation units

Browser simulations rarely use SI units directly — distances of 10⁻¹¹ m would underflow float precision. Use dimensionless or normalised units instead: set $G = 1$, mass in arbitrary units, and tune to achieve visually plausible behaviour.