Vectors, matrices, quaternions, ODE integration methods, and key physics constants — every formula you need when building or studying 3D browser simulations.
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.
| 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) |
Three.js stores matrices in column-major order in a
flat Float32Array. All transforms below produce 4×4
homogeneous matrices.
| 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 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)$.
| 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) |
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$.
| 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 |
| 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}$ |
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.