This project is a modular physics simulation environment developed using Java Swing. It provides an interactive platform to explore Newtonian Mechanics, Rigid Body Collisions, and Smoothed Particle Hydrodynamics (SPH) for fluid simulation.
The primary objective of this project is to demonstrate how complex physical phenomena can be modeled from scratch using pure Java, without relying on external physics libraries.
- Collision Resolution: Implements impulse-based collision logic with mass-weighted position correction to prevent particle overlapping.
- Momentum: Conservation of momentum for both elastic and inelastic collision scenarios.
- Environmental Interaction: Robust floor and wall collision detection.
- Fluid Dynamics: Real-time water simulation using the Smoothed Particle Hydrodynamics (SPH) algorithm.
- Internal Forces: Density-based Pressure (repulsion) and Viscosity (friction) calculations.
-
Spatial Hashing (Grid System): An optimized grid-based neighbor search system that reduces complexity from
$O(N^2)$ to approximately$O(N)$ . This enables fluid simulation of hundreds of particles with high performance.
- Real-time Parameters: On-the-fly adjustment of gravity, mass, and radius values.
- Dynamic Interaction: Ability to grab, drag, and toss particles using the mouse, or spawn new objects with the 'S' key.
- Modes: Seamless switching between individual rigid bodies and large liquid volumes.
- Integration: Utilizes the "Semi-implicit Euler" method for stable motion calculations.
- Vector Mathematics: All physical calculations are processed through a custom
Vector2library. - Performance: Proximity tests for SPH calculations are optimized via a custom
HashMap-based Grid system.
| Input | Action |
|---|---|
| Mouse Drag | Grabs and tosses particles. |
| 'S' Key | Spawns a rigid particle at a random location. |
| Gravity Field | Sets the global downward force (N). |
| Radius Field | Determines the size of objects (px). |
| Toggle Liquid | Clears the scene and initializes the SPH fluid simulation. |
This is an open-source educational project, and contributions are highly encouraged. If you would like to enhance the physics engine, improve performance, or add new features, please follow the guidelines below.
- Physics Engine Improvements: Implementation of inter-particle friction or Verlet Integration.
- Fluid Enhancements: Surface tension calculations or presets for different liquid types (e.g., honey, oil).
- Visualization: Integration with OpenGL (LWJGL) instead of Java 2D for advanced rendering.
- Bug Fixes: Improving edge-case handling in current collision detection algorithms.
- Fork the repository.
- Create a new branch for your feature (
git checkout -b feature/AdvancedPhysics). - Commit your changes (
git commit -m 'Add: Inter-particle friction'). - Push to the branch (
git push origin feature/AdvancedPhysics). - Open a Pull Request.
- Requirement: Ensure JDK 8 or higher is installed on your system.
- Compilation:
javac *.java - Execution:
java SimulationApp