A simple Verlet physics engine written in C++ using SFML.
- C++
- SFML 3
- CMake
cmake -S . -B build
cmake --build build
- Objects and obstacles
- Rainbow bouncy balls
- Ropes made of white balls
- 3 different sized square obstacles
- 2 different rectangles obstacles (vertical and horizontal)
- Performance
- Grid based partioning for balls and ropes
- Parallelization for obstacle collisions
- Other
- FPS, objects count, and obstacles count
- Collision detection for window boundaries
- Substepping for updating and collisions, to prevent overlap
- 3 different ways to launch balls: sin, random, and manual direction
Run the code and use settings.hpp to change constants. Pressing keys 1 through 5 spawns the obstacle where your mouse is.
| Control | Action |
|---|---|
| Hold left click | Spawn balls |
| Right click in two different places | Spawn rope |
| 1 | Small square |
| 2 | Medium square |
| 3 | Big square |
| 4 | Horizontal rectangle |
| 5 | Vertical rectangle |
| Backspace | Delete balls and rops |
| Enter | Delete obstacles |
| ESC | Close window |
- If the speed of the balls while initially spawning in is not great enough, the balls will collide and go in multiple different directions, instead of one steady stream.
- Certain combinations of
SLACKandSLACK_GAP_EXTRA_PXcreates tension which can make the rope have waves. However this can happen with real ropes too, such as a bungee coords, so I kept this in. Also, with higher values ofSLACKballs will clump together, but it can be fixed when shooting at it.
- To many balls in the window will cause them to "explode" but the window constraints will keep them in so they just bounce around super quickly.
This project was inspired from these two videos on youtube:
I thought this project was an important one to do because it combines performance and physics. I wanted to add more shapes, and implement the ropes, collision detection, and other aspects from the video by Pezzza's Network.
Newton's second equation of motion:
Our code uses deltaTime, or
velocity takes into account
Substituting this in,
Drop the SCALE in settings.hpp:
Simplify to get your standard Verlet integration formula:
In the code:
Vec2 velocity = ball.position - ball.previousPosition;
Vec2 newPosition = ball.position + velocity + acceleration * (deltaTime * deltaTime);
ball.previousPosition = ball.position;
ball.position = newPosition;






