Gravr is a real-time 2D physics simulation that models falling objects under the influence of gravity and air resistance. Built with C++ and SFML, the simulation supports accurate drag computation, collision physics, and user interaction through keyboard controls.
- Realistic gravitational motion using Newton's second law
- Drag force modeling based on Reynolds number and fluid dynamics
- Ball type classification based on mass input
- Ground collision with energy loss (coefficient of restitution)
- Real-time graphical display with height, velocity, and timer
- User controls for pausing, resuming, and resetting the simulation
- Adjustable mass (0.001–100 kg) and height (1–10 m)
F_gravity = m * g
g = 9.81 m/s²Drag is computed using the standard drag force equation:
F_drag = 0.5 * ρ * v² * C_d * AWhere:
ρ= air density (1.225 kg/m³)v= velocityC_d= drag coefficientA= cross-sectional area of the sphere
Re = (ρ * v * D) / μD= ball diameterμ= dynamic viscosity of air
| Regime | Condition | Equation |
|---|---|---|
| Stokes flow | Re < 0.1 | C_d = 24 / Re |
| Intermediate | 0.1 ≤ Re < 1000 | C_d = 24 / Re * (1 + 0.15 * Re^0.687) |
| Turbulent | 1000 ≤ Re < 300,000 | C_d = 0.44 |
| Post-critical | Re ≥ 300,000 | C_d = 0.1 |
Ground collisions apply a fixed coefficient of restitution (COR = 0.7):
v_after = -COR * v_beforeThe simulation ends when the bounce speed falls below 2.0 m/s.
Euler integration is used to update velocity and position:
acceleration = force / mass
velocity += acceleration * dt
position += velocity * dt| Key | Action |
|---|---|
| Enter | Start / Resume |
| Backspace | Pause |
| 0 | Reset |
| Escape | Exit |
Gravr assigns a label based on the selected mass:
| Mass Range (kg) | Ball Type |
|---|---|
| 0.001 – 0.003 | Ping pong |
| 0.003 – 0.05 | Racquet ball |
| 0.05 – 0.065 | Tennis ball |
| 0.065 – 0.16 | Baseball |
| 0.16 – 0.5 | Soccer ball |
| 0.5 – 0.7 | Basketball |
| 0.7 – 8.0 | Bowling ball |
| 8.0 – 20.0 | Light medicine ball |
| 20.0 – 50.0 | Heavy medicine ball |
| 50.0 – 100.0 | Industrial ball |
gravr/
├── src/
│ ├── main.cpp # Application entry point
│ ├── Simulation.cpp/.h # Main simulation loop and logic
│ ├── Particle.cpp/.h # Physics calculations and particle state
│ ├── UIManager.cpp/.h # User interface and rendering
│ └── InputHandler.cpp/.h # User input processing
├── assets/fonts/
| └── RobotoMono-Regular.ttf # Font used
├── build/ # Build directory (created by CMake)
├── CMakeLists.txt # Build configuration
└── README.md
- C++17-compatible compiler
- CMake 3.10 or higher
- SFML 3.0.0 or higher
sudo apt update
sudo apt install build-essential cmake libsfml-devsudo pacman -Syu
sudo pacman -S base-devel cmake sfmlgit clone https://github.com/chitvs/gravr.git
cd gravr
mkdir build && cd build
cmake ..
make
./bin/main- Open the project in VS Code
- Run
CMake: Buildfrom the Command Palette (Ctrl + Shift + P) - Execute the built binary from the
build/directory
const float PIXELS_PER_M = 57.78f;
const float GRAVITY = 9.81f;
const float AIR_DENSITY = 1.225f;
const float AIR_VISCOSITY = 1.81e-5f;
const float BALL_RADIUS = 0.04f;
const float DRAG_MULTIPLIER = 8.0f;This project is licensed under the MIT License. The included Roboto font is licensed under the Apache License 2.0.
