N disks bounce elastically off the walls of a 2D box and off each other. Every collision is a real elastic scatter between equal masses along the line of centers:
v1' = v1 − [(v1−v2)·(x1−x2) / |x1−x2|²] (x1−x2)
v2' = v2 − [(v2−v1)·(x2−x1) / |x2−x1|²] (x2−x1)
This exchange conserves total momentum and total kinetic energy exactly — nothing in the microscopic rule is irreversible. The box starts in a low-entropy state: every disk launched with (nearly) the same speed, just random directions. As collisions randomize the speeds, they spread toward the 2D kinetic-theory equilibrium.
Why 2D changes the target distribution: a 2D gas has 2 degrees of freedom per particle instead of 3, so equipartition gives ⟨v²⟩ = 2σ² (not 3σ²), and the equilibrium speed distribution is the Rayleigh distribution f(v) = (v/σ²)·e−v²/2σ², not the 3D Maxwell–Boltzmann curve — σ² equals the average per-particle kinetic energy. The histogram panel draws the live speed histogram against this theoretical curve so you can watch the gas approach it. (Verified numerically: a standalone 20,000-step run of this exact collision rule converged to within 0.5% of the predicted Rayleigh mean speed and exactly matched ⟨v²⟩ = 2σ².)
Entropy is measured the way Boltzmann and Gibbs defined it — coarse-grain the disk speeds into K bins, read off the occupation fractions pk, and sum:
S = −Σ p_k ln p_k (k_B = 1, this is Boltzmann's H-function with a sign flip: S = −H)
Second law: dS/dt ≥ 0 for an isolated system
Energy stays flat while entropy climbs — that is the whole point. Reversing every velocity would, in principle, walk the system back toward its ordered start, but the odds of that happening by chance are astronomically small once N is large: that asymmetry between a reversible rule and an irreversible outcome is the statistical arrow of time.
Self-gravity adds a soft pairwise attraction, integrated with a semi-implicit (symplectic) Euler step so total energy (kinetic + gravitational potential) stays approximately conserved even as the gas is no longer uniform. Switching it on lets you watch order re-emerge locally — disks clump into transient clusters — without violating the second law globally: local order can still grow as long as total entropy of the whole system does not fall.
Drag inside the box to pan, scroll/pinch to zoom — the physics runs independently of the view.