Wave 55: Polyhedra Explorer, Fish School 3D & SPH Dam Break

A geometric solid viewer, a Boids flocking simulation with a hunting shark, and a real-time SPH dam-break โ€” three very different explorations of mathematics, collective behaviour, and computational fluid dynamics added to mysimulator.uk in wave 55.

Wave 55 at a glance

3
new simulations
6
HTML files
541
total simulations
1
blog post
๐Ÿ”ท

Polyhedra Explorer

All 5 Platonic and 13 Archimedean solids rendered with perspective projection. Drag to rotate, scroll to zoom, toggle wireframe and vertex labels. Euler's formula V−E+F=2 verified live.

Open simulation →
๐ŸŸ

Fish School 3D

200 fish governed by Boids separation, alignment, and cohesion with three depth layers for pseudo-3D realism. A shark predator hunts the school, triggering panic-flee behaviour within 110 px.

Open simulation →
๐Ÿ’ง

SPH Dam Break

300 particles governed by SPH pressure, viscosity, and gravity. A dam of water collapses and washes across the container. Colour by speed, pressure, or density.

Open simulation →

๐Ÿ”ท Polyhedra Explorer

Polyhedra โ€” three-dimensional solids bounded by flat polygonal faces โ€” are among the most studied objects in mathematics. The ancient Greeks classified the five Platonic solids: convex polyhedra whose faces are all congruent regular polygons and whose vertices are all equivalent. Archimedes extended this to 13 Archimedean solids: convex polyhedra with two or more types of regular polygon faces but still vertex-transitive.

Euler's polyhedron formula

Leonhard Euler (1752) discovered that for any convex polyhedron โ€” indeed for any simply-connected polyhedral surface homeomorphic to a sphere โ€” the counts of vertices V, edges E, and faces F satisfy:

V − E + F = 2 โ€” a topological invariant, not a geometric one. The tetrahedron gives 4−6+4=2; the dodecahedron gives 20−30+12=2; a truncated icosahedron (soccer ball) gives 60−90+32=2. The number 2 is the Euler characteristic of the sphere.

The formula generalises: for a surface of genus g (a torus has g=1,) the Euler characteristic is 2−2g. A torus satisfies V−E+F=0. This connection between combinatorial properties (vertex/edge/face counts) and topological properties (genus) is a cornerstone of algebraic topology.

The five Platonic solids and their symmetry groups

The Platonic solids correspond to the finite rotation groups of 3D space. Their symmetry groups are the same as those of the rotational symmetries of these solids:

There are only five because the requirement for a regular convex solid limits the possible combinations of regular polygon faces meeting at a vertex: the sum of face angles at each vertex must be strictly less than 360ยฐ. Only triangles, squares, and pentagons produce valid solutions before this angular deficit runs out.

Perspective projection on Canvas 2D

The simulator uses a pure Canvas 2D perspective projection: each 3D vertex (x, y, z) is first rotated by two quaternion-derived matrices (yaw and pitch, updated each drag tick), then projected to screen with sx = cx + fยทX/(Z+d), sy = cy + fยทY/(Z+d) where f is the focal length and d is the camera offset. Faces are painter-sorted by their centroid Z coordinate to give correct depth ordering.

๐ŸŸ Fish School 3D — Boids with Predator

In 1986 Craig Reynolds published Boids โ€” a deceptively simple model that produces strikingly lifelike flocking behaviour from just three local rules. No global coordinator directs the flock; each agent reacts only to its neighbours within a perception radius. The emergent result looks indistinguishable from a real starling murmuration or anchovy school.

The three Boids rules

The net steering force at each tick is a weighted sum:

F = wsepยทFsep + waliยทFali + wcohยทFcoh โ€” then clamped to a maximum acceleration. The velocity is updated with Euler integration and normalised to the target speed window [vmin, vmax].

Depth layering for pseudo-3D

Canvas 2D runs a 2D Boids simulation, but three depth layers simulate the three-dimensional structure of a real fish school. Surface fish (layer 0) are drawn larger and in lighter cyan; mid-water fish (layer 1) are medium-sized in ocean blue; deep fish (layer 2) are smaller and dark navy. The layers use slightly different Boids radii so they maintain distinct sub-schools while still influencing each other through the cohesion and alignment radii.

The predator shark

The shark is a single agent with a dedicated hunting behaviour:

The predator-prey dynamic in Boids models reproduces several field observations: (a) schools split into fragments when the predator enters, then reform after the predator leaves โ€” the fountain effect; (b) the predator has most success attacking solitary or peripheral fish; (c) a school of N fish in tight formation offers each member only 1/N the predation probability of a solitary fish โ€” the dilution effect.

Cluster counting

The stats panel reports the number of distinct clusters each frame. Cluster membership is determined by a simple union-find: two fish are in the same cluster if their mutual distance is below the cohesion radius. During normal flocking the count is typically 1โ€“3; during a shark attack it often spikes to 6โ€“10 as the school fragments.

๐Ÿ’ง SPH Dam Break — Smoothed Particle Hydrodynamics

Smoothed Particle Hydrodynamics (SPH) was invented simultaneously and independently by Lucy (1977) and Gingold & Monaghan (1977) to simulate astrophysical phenomena such as stellar collisions. Unlike grid-based (Eulerian) methods, SPH is fully Lagrangian: the fluid is discretised into particles that carry mass, velocity, and thermodynamic variables. No mesh is required; the particles themselves define the computational domain.

The SPH approximation

Any field quantity A at position r can be approximated as a weighted sum over neighbouring particle contributions:

A(r) โ‰ˆ ฮฃโฑผ (mโฑผ/ฯโฑผ) Aโฑผ W(|rโˆ’rโฑผ|, h) โ€” where W is a smoothing kernel and h is the smoothing length. The Poly6 kernel W(r,h) = (315/64ฯ€hโน)(hยฒโˆ’rยฒ)ยณ is used for density, while the Spiky kernel gradient (Munafo 1992, as corrected by Desbrun 1996) is used for pressure to avoid particle clustering: โˆ‡Wspiky = โˆ’(45/ฯ€hโถ)(hโˆ’r)ยฒ rฬ‚.

Pressure and viscosity forces

First, density is computed for every particle from the Poly6 sum. Then pressure is derived from a simple equation of state:

p = k(ฯ โˆ’ ฯโ‚€) โ€” a linearised Tait equation. k is the pressure stiffness (compressibility inverse); ฯโ‚€ is the rest density. Larger k makes the fluid less compressible at the cost of requiring smaller timesteps.

The pressure force on particle i from all neighbours j is:

Fpres = โˆ’mโฑผ (pแตข+pโฑผ)/(2ฯโฑผ) โˆ‡Wspiky โ€” the symmetric average (pแตข+pโฑผ)/2 ensures conservation of linear momentum. Viscosity uses the Laplacian kernel โˆ‡ยฒWvisc = (45/ฯ€hโถ)(hโˆ’r) to smooth velocity differences between particles.

Stability: substeps and damping

The simulation runs 3 substeps per animation frame with a fixed dt = 0.004 s. Boundary elastic reflection uses a damping factor of 0.4 so kinetic energy is removed on wall contact โ€” modelling the inelastic nature of real concrete/fluid boundaries. For N ≤ 600 the O(Nยฒ) neighbour search runs in a single JS loop at 60 fps on modern hardware; grid-acceleration (spatial hashing) would extend the cap to ~5000 particles.

Real-world applications of SPH: water simulation in Half-Life 2 and many modern game engines; coastal flood forecasting and tsunami run-up models; crash-test simulation of fuel tanks; astrophysical binary-star mergers and supernova ejecta; blood-flow simulation in cerebral aneurysms; metal shaping and high-velocity impact in aerospace engineering.
← Devlog #74: Van der Pol Sync, Bacterial Chemotaxis & Genetic Evolution Blog index →