Overview & Mathematical Foundations
The Interactive Life Simulation platform provides an advanced computational sandbox designed to explore multi-phase granular physics, Newtonian fluid flow, static spatial boundaries, and non-linear biological cellular automata. Built upon discrete grid spatial discretization models, the simulator resolves complex physical interactions between disparate particulate states in real-time. By representing granular matter (Sand), low-viscosity fluids (Water), unyielding barriers (Rock), and self-replicating organic elements (Life), this framework demonstrates how simple local neighborhood rules give rise to emergent macroscopic behaviors such as sediment transport, hydro-sloshing, percolation, and self-organized biological equilibrium.
At its mathematical core, the system models state updates across a 2D discrete grid lattice $\Omega = \{(x, y) \mid 0 \le x < N_{\text{cols}}, 0 \le y < N_{\text{rows}}\}$. Each cell at coordinate $(x, y)$ holds a discrete state value $S_t(x, y) \in \{0, 1, 2, 3, 4\}$ corresponding to Empty, Sand, Water, Rock, and Life elements respectively. The transition function $f: S_t(x, y) \to S_{t+1}(x, y)$ computes state transformations over discrete time intervals $\Delta t$ according to local neighborhood states $\mathcal{N}(x, y)$:
$$S_{t+1}(x, y) = f\big(S_t(x, y), \mathcal{N}(S_t(x, y))\big)$$
Granular materials and fluids obey density-displacement conservation principles where relative material densities dictate buoyancy and displacement ordering $\rho_{\text{rock}} > \rho_{\text{sand}} > \rho_{\text{water}} > \rho_{\text{empty}}$. When high-density sand particles fall onto fluid cells, displacement updates invert particle coordinates to model buoyant displacement. Biological life dynamics incorporate standard John Conway Game of Life rules (B3/S23) augmented by a stochastic germination function tied to local hydration levels:
$$\sigma(x, y) = \sum_{dx=-1}^{1} \sum_{dy=-1}^{1} \mathbb{I}\big(S_t(x+dx, y+dy) = \text{LIFE}\big) - \mathbb{I}\big(S_t(x, y) = \text{LIFE}\big)$$
$$S_{t+1}(x, y) = \begin{cases}
\text{LIFE} & \text{if } S_t(x, y) = \text{LIFE} \text{ and } \sigma(x, y) \in \{2, 3\} \\
\text{LIFE} & \text{if } S_t(x, y) = \text{EMPTY} \text{ and } \sigma(x, y) = 3 \\
\text{LIFE} & \text{if } S_t(x, y) = \text{EMPTY} \text{ and } \mathcal{H}(x, y) = 1 \text{ with prob } P_{\text{growth}} \\
\text{EMPTY} & \text{otherwise}
\end{cases}$$
where $\mathbb{I}(\cdot)$ represents the indicator function and $\mathcal{H}(x, y)$ denotes adjacent stable resting fluid context.
Technical Architecture & Computational Safeguards
The visual engine operates via a high-performance 1D Uint8Array flat array buffer representing the grid matrix ($I = y \cdot N_{\text{cols}} + x$). Linearizing multi-dimensional grid arrays eliminates JavaScript object allocation overhead and garbage collection pauses, enabling sub-millisecond physics iterations. Double-buffering via Uint8Array.prototype.set() guarantees update step isolation and eliminates spatial scan-line directional bias artifacts.
Canvas rendering utilizes direct pixel-buffer manipulation via 2D context ImageData blitting inside a synchronized requestAnimationFrame loop thread. Buffer dimensions are decoupled from DOM canvas size scaling using a dedicated ResizeObserver on the container element, preventing visual flickering and avoiding context re-initializations during 30 FPS or 60 FPS update steps. Solid-state boundary wedges prevent diagonal seam leakage in cellular sand mechanics. Non-finite mathematical safety checks ($\text{isNaN}$, $\text{isFinite}$) shield simulation steps from numerical divide-by-zero traps during high-density state changes.
Audio feedback utilizes Web Audio API gain nodes connected to dynamic sine-wave oscillators. Oscillator pitch maps directly to vertical touch coordinate height $y$, decaying exponentially ($V(t) = V_0 e^{-t/\tau}$) upon particle placement events to avoid audio clipping. State isolation during automated Demo Mode utilizes isolated deep-copy snapshots (`structuredClone`), preserving user settings and allowing seamless instant breakout upon physical user interaction.
Future Development & Roadmap
Planned visual engine releases will incorporate GPU compute shaders via WebGL/WebGPU to expand active cellular resolution to over $1,000 \times 1,000$ active nodes at 60 FPS. Advanced thermodynamic coupling will allow high-temperature material transformations—melting sand into molten glass or boiling liquid water into expanding vapor particles upon contact with thermal rock elements. Additional fluid parameters including viscosity coefficients, surface tension equations, and multi-fluid mixing will further expand the platform's diagnostic utility for educational physics laboratories.