In physics, an elastic collision is an encounter (collision) between two bodies in which the total kinetic energy of the two bodies remains the same. In an ideal, perfectly elastic collision, there is no net conversion of kinetic energy into other forms such as heat, noise, or potential energy.
This C++ project implements an elastic collision physics simulation using the SFML (Simple and Fast Multimedia Library). The simulation includes balls bouncing within a confined space, and users can manipulate gravity, restitution, and restart the simulation. This project was coded and tested with Ubuntu 22.04 LTS.
A collision in two dimensions obeys the same rules as a collision in one dimension. Total momentum in each direction is always the same before and after the collision. Total kinetic energy is the same before and after an elastic collision. Note that the kinetic energy is not calculated for each direction separately but depends on the magnitude of the total velocity of each object.
- Restitution = 1 indicates an elastic collision.
- 0 < Restitution < 1 indicates a partially elastic collision.
- Restitution = 0 indicates a perfectly inelastic collision.
- -1 < Restitution < 0 indicates a collision with some degree of "stickiness."
- Restitution = -1 indicates a perfectly sticky collision.
- C++ Compiler
- SFML library (Simple and Fast Multimedia Library)
- optional Makefile (for much easier compilation)
- Clone the repository:
git clone https://github.com/raphsenn/elastic-collision
- Install SFML by following the instructions on the official SFML website.
https://www.sfml-dev.org/tutorials/2.6/start-linux.php (linux)
https://www.sfml-dev.org/tutorials/2.6/start-osx.php (macOS)
https://www.sfml-dev.org/tutorials/2.6/start-vc.php (windows)
- Compile the project:
make
- Run the executable:
./main
- Clone the repository:
git clone https://github.com/raphsenn/elastic-collision
- Install SFML by following the instructions on the official SFML website. Just download SFML folder and put it into this folder.
https://www.sfml-dev.org/tutorials/2.6/start-linux.php (linux)
https://www.sfml-dev.org/tutorials/2.6/start-osx.php (macOS)
https://www.sfml-dev.org/tutorials/2.6/start-vc.php (windows)
- Create the build directory:
mkdir -p build
- Compile the project:
g++ -std=c++11 -Wall -Wextra -pedantic -c src/ball.cpp -o build/ball.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/button.cpp -o build/button.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/main.cpp -o build/main.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/random.cpp -o build/random.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/sandbox.cpp -o build/sandbox.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/slider.cpp -o build/slider.o -Iinclude
g++ -std=c++11 -Wall -Wextra -pedantic -c src/vector2.cpp -o build/vector2.o -Iinclude
- Linking (dont forget the sfml flags!)
g++ -std=c++11 -Wall -Wextra -pedantic -o main build/ball.o build/button.o build/main.o build/random.o build/sandbox.o build/slider.o build/vector2.o -lsfml-graphics -lsfml-window -lsfml-system
- Run the executable
./main
-
vector2.h
andvector2.cpp
: Define and implement theVector2
class, providing vector operations. -
sandbox.h
andsandbox.cpp
: Manage the physics simulation using theSandBox
class. -
ball.h
andball.cpp
: Define and implement theBall
class, representing bouncing balls in the simulation. -
random.h
andrandom.cpp
: Implement utility functions for generating random values used in the simulation. -
slider.h
andslider.cpp
: Handle slider UI elements with theSlider
class. -
button.h
andbutton.cpp
: Handle UI elements with theButton
class. -
main.cpp
: Serve as the entry point for the program.