2D & 3D Physics Solvers & Numerical Integration
โ๏ธ 2D & 3D Physics Solvers & Numerical Integration
In custom game engines (built with Odin and Vulkan), physics engines simulate motion, collisions, and constraints through Numerical Integration.
Understanding the trade-offs between Euler Integration, Verlet Integration, and Runge-Kutta (RK4) allows you to choose the right solver for rigid bodies, cloth, particles, and vehicles.
๐งฎ 1. Numerical Integration Methods
Integration computes an entity's new position
+-------------------------------------------------------------+
| 1. Explicit / Forward Euler (Fastest, Highly Unstable) |
| v += a * dt; x += v * dt; |
+-------------------------------------------------------------+
โ (Accumulates energy / explodes)
โผ
+-------------------------------------------------------------+
| 2. Semi-Implicit / Symplectic Euler (Standard for Game Dev) |
| v += a * dt; x += v_new * dt; |
+-------------------------------------------------------------+
โ (Preserves energy over time)
โผ
+-------------------------------------------------------------+
| 3. Verlet Integration (Ideal for Cloth, Ropes, Ragdolls) |
| x_new = 2*x - x_old + a * dt^2; |
+-------------------------------------------------------------+
Semi-Implicit Euler Implementation in Odin
integrate_body :: proc(body: ^Rigid_Body, gravity: [3]f32, dt: f32) {
if body.is_static do return
// 1. Calculate acceleration from forces
inv_mass := 1.0 / body.mass
acceleration := (body.force * inv_mass) + gravity
// 2. Update velocity first
body.velocity += acceleration * dt
body.velocity *= (1.0 - body.linear_drag * dt) // Simple damping
// 3. Update position using the NEW velocity
body.position += body.velocity * dt
// Reset accumulated forces for next tick
body.force = {0, 0, 0}
}
๐งต 2. Verlet Integration & Position-Based Dynamics (PBD)
Verlet integration does not store velocity explicitly. Instead, velocity is implicitly derived from the difference between the current position and the previous position:
Why Verlet Excels for Constraints (Cloth & Ropes)
When simulating distance constraints (e.g. rope links or cloth springs), Verlet allows you to simply project positions directly to satisfy distance limits without modifying velocity states:
solve_distance_constraint :: proc(p1, p2: ^Particle, target_distance: f32) {
delta := p2.position - p1.position
current_dist := linalg.length(delta)
if current_dist == 0.0 do return
// Percentage error
diff := (current_dist - target_distance) / current_dist
// Push particles apart or pull together
p1.position += delta * 0.5 * diff
p2.position -= delta * 0.5 * diff
}
๐ฆ 3. Broad-Phase vs Narrow-Phase Collision Detection
Simulating 1,000 entities naively requires
[ All Scene Entities ] โโ> [ Broad-Phase: Spatial Hash / BVH / AABB ]
โ (Prunes 99% of pairs)
โผ
[ Narrow-Phase: SAT / GJK / EPA ]
โ (Exact contact points & normals)
โผ
[ Impulse Solver / Manifold Resolution ]
- Broad-Phase (Fast & Rough):
- Uses AABB (Axis-Aligned Bounding Boxes) organized in a Dynamic Bounding Volume Hierarchy (BVH) or Spatial Grid/Hash.
- Quickly rejects objects that are nowhere near each other.
- Narrow-Phase (Exact & Geometric):
- SAT (Separating Axis Theorem): Tests whether a separating plane exists between convex 2D/3D polyhedra.
- GJK (Gilbert-Johnson-Keerthi): Finds the minimum distance between arbitrary convex shapes in 3D using Minkowski differences.
- EPA (Expanding Polytope Algorithm): Computes penetration depth and contact normal when GJK detects overlap.
โก 4. Sequential Impulse Resolution (Penetration & Restitution)
Once a collision manifold with normal
- Where
is the Coefficient of Restitution ( inelastic/clay, superball). - Apply velocity delta:
and .
๐ Related Notes
- Odin Programming Language Index โ Systems language used for low-level physics engines.
- Precision โ Handling float drift and rounding errors in physics solvers.
- Optimization โ Eliminating broad-phase allocations and cache-friendly struct layouts.
- Client-Side Prediction & Server Reconciliation โ Deterministic physics state replay across network ticks.
- Game Development Hub โ Central game development index.