Posted October 22, 2025 by aNoobdev
#sandbox #gravity #simulation #physics
Gravity Sandbox is a 2D real-time n-body gravitational simulation sandbox built using Java and Swing, focusing on minimal UI and high-precision motion physics.
The game allows users to create self-evolving systems of celestial bodies and observe stable or decaying orbital structures through emergent behavior.
| System | Description |
|---|---|
| Gravity Model | Newtonian point-mass attraction using inverse square law |
| Integration | Continuous real-time step-based numerical integration |
| Interaction | Dynamic body creation, deletion, and repositioning |
| Stability | Self-stabilizing orbits emerge naturally based on velocity, mass, and radius |
| Rendering | Lightweight particle-based glow effects with motion trails |
The simulation operates in multiple tool modes, toggleable by keyboard shortcuts:
| Mode | Function |
|---|---|
| 1 | Add stars/planets with mass & initial velocity |
| 2 | Add and drag existing bodies to modify orbits |
| 3 | Create white holes (negative mass / repulsive gravity) |
| 4 | Delete bodies from the scene |
These modes allow the user to repeatedly edit and reshape the gravitational system during runtime without resetting the simulation.
| Aspect | Details |
|---|---|
| Language | Java |
| UI Framework | Swing (lightweight, custom rendering) |
| Assets | External raster glow-based sprites and trail rendering |
| Physics Tick | Fixed timestep loop tied to repaint cycle |
| Input | Mouse drag for placement and trajectory initialization |
| Memory Usage | Minimal (no heavy engines or GPU APIs) |
| JDK | Distributed with custom minimal runtime (jlink) |
Bodies rendered with layered soft glow effect to simulate stellar luminosity
Motion trails generated by storing past path history (short-lived)
Black cosmic background for contrast and realism
No UI clutter — simulation is the visual focus
The game does not rely on predefined scripted systems.
Every orbital path, collision, decay, or ejection is a natural consequence of real-time gravitational math.
You design systems, not animations.
Body-to-body collisions (merge / fragmentation)
Adjustable gravitational constants
Save/load system states
Procedural galaxy generation
UI sliders for live parameter tuning