Sph
Static helpers for SPH simulation and uniform-grid neighbor queries.
Methods
static u64 cellKey(s32 ix, s32 iy, s32 iz)
Packs integer cell coordinates into a single 64-bit key.
| ix | Cell index along X | |
| iy | Cell index along Y | |
| iz | Cell index along Z |
static f32 smoothingKernel(f32 dist, f32 h)
Linear SPH kernel W(r,h) = max(0, 1 - r/h) / h
| dist | Distance between particles | |
| h | Smoothing length |
static Vector3 gradientSmoothingKernel(const Vector3& distVec, f32 dist, f32 h)
Gradient of the linear kernel potential used in the JS reference. Algebraically identical to (distVec / |distVec|) * (-|distVec| / h^2) with the magnitudes cancelling: no sqrt, no normalize, no temporaries.
| distVec | thisPosition - neighborPosition | |
| dist | distVec | |
| h | Smoothing length |
static void step(Particle** particles, u32 n, const f32* positions, f32* density, f32* pressure, f32 h, f32 k, f32 rho0, f32 viscosity, vector<vector<u32>>& cells, const Vector3& halfExtent)
One SPH pass: clears forces, builds the flat grid, density, pressure, then pressure + viscous forces. Reads positions from the contiguous positions[] array (must be in sync with curState.position at call time). Writes pressure and viscous forces into particles[i]->forces. The caller's integration step is expected to add gravity and divide forces by mass to produce acceleration.
| particles | Particle pointers (reads mass and curState.velocity, writes forces) | |
| n | Count | |
| positions | Contiguous position buffer (length n*3, xyz interleaved) | |
| density | Per-particle density (length n) | |
| pressure | Per-particle pressure (length n) | |
| h | Smoothing length (cell size = h) | |
| k | Pressure stiffness (JS default 1000) | |
| rho0 | Rest density (JS default 1000) | |
| viscosity | Viscosity scale (JS default 0.1) | |
| cells | Reused flat cell array (resized to nxnynz internally) | |
| halfExtent | Half-size of the AABB the simulation lives in |