Three.js Traffic Flow Simulation (Phantom Traffic Jams)

Ring-road traffic in Three.js using the Intelligent Driver Model, where tiny perturbations grow into stop-and-go phantom jams, shown with speed-colored cars and a live space–time diagram.

Key topics: three.js traffic simulation, intelligent driver model, phantom traffic jam, sugiyama experiment, traffic flow

What this code does

- Intelligent Driver Model: acceleration a[1 − (v/v₀)⁴ − (s*/s)²] balances free-road speed against a safe dynamic gap s*.
- Sugiyama Experiment: like the 2008 ring-road study, cars spaced evenly still develop jams with no bottleneck at all.
- String Instability: with gentle acceleration (0.5 m/s²) small speed fluctuations amplify down the line of cars.
- Space–Time Diagram: the infield scrolls each car's position over time, and red jam bands drift backwards against traffic.
- Real-World Units: one world unit is 10 m, so the HUD reports density in vehicles/km and mean speed in km/h.
- Controls: car count, desired speed, time headway, minimum gap, acceleration, braking, driver noise, time scale and "tap the brakes".

Continue Learning

JavaScript Implementation

ES6+
// Intelligent Driver Model on a ring road
const n = 40
const L = 75 // ring length
const pos = Array.from({ length: n }, (_, i) => (i / n) * L)
const vel = new Array(n).fill(1.5)
const v0 = 3
const T = 1.0
const s0 = 0.2
const a = 0.05
const b = 0.15
const carLength = 0.45

function step (dt) {
  const acc = pos.map((x, i) => {
    const lead = (i + 1) % n
    let gap = pos[lead] - x - carLength
    if (gap < -carLength) gap += L
    const v = vel[i]
    const sStar = s0 + Math.max(0, v * T + v * (v - vel[lead]) / (2 * Math.sqrt(a * b)))
    return a * (1 - (v / v0) ** 4 - (sStar / Math.max(gap, 0.01)) ** 2) + (Math.random() - 0.5) * 0.01
  })
  acc.forEach((ai, i) => {
    vel[i] = Math.max(0, vel[i] + ai * dt)
    pos[i] = (pos[i] + vel[i] * dt) % L
  })
}

// Each frame: step(1 / 60), then place car i at angle pos[i] / R on a circle of radius R = L / (2π)
// and color it from red (stopped) to green (v0) to watch phantom jams travel backwards.