Java implementation of smoothed particle hydrodynamics (SPH), to simulate the collisions of planetary scale bodies in realtime using CUDA (~32,000 particles at 60 FPS on an RTX 4090, 64,000 at 30 FPS).
A snapshot from a collision simulation, shortly following collision of a small mass with an Earth-sized mass.
Moon formation simulation: planet Theia impacting the proto-Earth at a 45 degree angle at 8km/s.
SPH simulates matter in four steps:
- Matter is assumed to have continuous fields like density, pressure, and velocity.
- The continuum is divided into a finite collection of discrete elements, "particles".
- These particles interact pairwise (n^2), each particle influencing their neighbors weighted by a special "smoothing kernel", which weights more distant interactions weakly.
- The sum of these interactions provides a net force determined through the Euler-Lagrange equations. This is then integrated over time to compute new velocities and positions on the next timestep (a finite subdivision of time, which is also assumed to be continuous). This is repeated throughout the simulation.
Main.java contains the rendering pipeline and dispatches to SPH_GPU. gpulib/SPH_GPU.java is the core of the SPH simulation.
Currently, to execute, you must clone the repository and run from source code (I am in the process of building a JAR release file). JDK version 19 was used for development. Keybinds:
- PAUSE BREAK = Pause time.
- UP/DOWN Arrows = Accelerate/Deaccelerate time (may destabilize the simulation if increased too far).
- BACKSPACE = Reverse time.
- Right click = Orbit camera target.
- Shift + Right click = Translate camera left/right/up/down relative to the camera.
- Scroll = Zoom out
- 0 = Zero all velocities.