![]() |
Interactive physics simulation of elastic collisions between multiple spherical bodies in 2D space. Built with OpenGL 4.6, GLFW, PyGLM, numpy, and ImGui. |
|---|
Key Features:
- N-body simulation with random initial parameters
- Elastic collision detection and resolution
- Spatial grid optimization
- Boundary reflections with energy conservation
- Real-time gravity vector control (X and Y axes)
- Dynamic velocity-based coloring
- Interactive color gradient editing
- Real-time velocity threshold adjustment
- Pause/resume and speed control
- Adaptive aspect ratio handling
- FPS display
Represents a physical body with position, velocity, acceleration, mass, size, and colors. Handles position updates, boundary collisions, dynamic coloring, and collision detection/resolution between pairs.
Implements spatial partitioning for collision optimization. Divides space into cells and maintains body references per cell, reducing collision checks from O(n²) to O(n) average.
Core simulation manager. Initializes bodies, manages collision detection via spatial grid, updates simulation state, applies global forces (gravity), handles aspect ratio changes, and provides data for visualization.
ImGui-based user interface with GLFW backend. Provides pause/resume, speed control, gravity sliders (X/Y), dynamic coloring toggle, velocity threshold sliders, and color gradient selectors.
OpenGL rendering layer. Compiles shaders, manages GPU buffers (VAO, VBO), updates buffers with simulation data, configures rendering pipeline (blending, antialiasing), and renders particles as points.
Shaders:
- Vertex: Transforms positions, sets size and color
- Fragment: Draws smooth circles with alpha blending and antialiasing
Application entry point. Initializes GLFW window with OpenGL 4.6 Core Profile and 4x MSAA, creates Simulation/GUI/Renderer instances, runs main loop (events → update → render), calculates deltaTime and FPS, handles window resize with aspect ratio updates, and manages cleanup.
- Python 3.14.0+
- OpenGL 4.6 support
pip install -r requirements.txtpython main.py [-width WIDTH] [-height HEIGHT] [-particles PARTICLES]Parameters (all optional):
-width: Window width in pixels (default: 800, min: 400, max: 2560)-height: Window height in pixels (default: 700, min: 300, max: 1440)-particles: Number of bodies (default: 1500)
Example:
python main.py -width 2137 -height 1000 -particles 1500Control Panel (top-left):
-
Pause/Resume Button
- Pauses/resumes physics simulation
- Rendering continues during pause
-
Gravity Vector
- Horizontal (X): -0.1 to 0.1 (default: 0.0)
- Vertical (Y): -0.1 to 0.1 (default: 0.0, negative = downward gravity)
-
Visual Options
- Color based on speed: Toggle dynamic coloring
- Min speed threshold: Lower velocity bound for gradient
- Min color selector: Color for slow bodies (default: blue [0,0,1])
- Max speed threshold: Upper velocity bound for gradient
- Max color selector: Color for fast bodies (default: red [1,0,0])
Window:
- Resize supported (400x300 to 2560x1440)
- FPS displayed in title bar
Main parameters (main.py):
SCR_WIDTH = 1000
SCR_HEIGHT = 500
N = 1500Body initialization ranges (simulation.py):
- Position X:
(-0.9 * aspect_ratio, 0.9 * aspect_ratio) - Position Y:
(-0.9, 0.9) - Initial velocity:
(-0.2, 0.2)per axis - Size: Dynamically scaled based on body count
- Mass:
(max_radius, max_radius * 2) - RGB colors:
0.3to1.0per channel
Color visualization (gui.py):
- min_speed_threshold:
0.01 - max_speed_threshold:
0.4 - Gradient: blue (slow) → purple (medium) → red (fast)
- Complete elastic collision physics with spatial grid optimization
- Advanced UI with real-time parameter control
- Interactive gradient editing with velocity thresholds
- Speed control (including reverse playback)
- Modern OpenGL 4.6 rendering with custom shaders
- Dynamic window resizing with aspect ratio adaptation
- Velocity-based dynamic coloring
- Body size scaling based on particle count
- Command-line parameterization
- Comprehensive documentation
- Quadtree spatial partitioning for O(n log n) collisions
- Multithreading with Numba (requires data-oriented refactoring)
- Multiple object types (static walls, polygons)
- Mouse interaction (add/remove/drag objects)
- Save/load simulation states
- Advanced visualization filters (trajectories, force vectors)
- 3D physics support
