All writing

C++WebAssemblySimulation

Phase Transition demo

A 2D particle simulator written in C++ and compiled to WebAssembly, where solid, liquid and gas-like states of matter emerge from the physics.

Nothing in this simulation knows what a liquid is. Particles simply attract and repel each other, and the familiar states of matter — solid, liquid and gas — emerge from that. Heat the particles up and they fly apart; cool them down and they clump together.

The simulator is written in C++ and compiled to WebAssembly, so the physics below runs in your browser. Grab it with the mouse:

c create  s spray  p push  h heat Left button applies the mode, right button reverses it Mouse wheel resizes the range of influence

Use your mouse to create and influence the particles. There are four modes of interaction: creating, spraying, pushing and heating, each of which can be activated with keys c, s, p and h respectively. Once in a mode, this action can be carried out on particles by holding the left mouse button. The right mouse button does the opposite of the activated mode’s function — so in heating mode it cools the particles down, which is how you condense a gas into a liquid.

The number of particles, average velocity, and average temperature (inside the range of influence) are displayed in the upper left corner of the display.

Currently, the demo only uses a single core due to compatibility reasons with most browsers. Performance suffers — the native build is considerably faster.

Source code is on GitHub, and there’s a video of the simulation running natively, with rather more particles than a browser will tolerate.