Elastic Collision Calculator – Simulate an elastic collision: given input masses and initial velocities for two objects colliding elastically in one dimension, animate the interaction and calculate their final velocities after the collision. Which method of solving elastic collisions did you use? Explain why. Assessment criteria:
- Code is clear with comments and can accept different inputs
- Output velocities are clear and correct
- Animation is clear and correct~~
- Calculations to verify the accuracy of the output are correct
- Explanation of method is clear and concise
The project contains 6 files created by me. 4 are graphics related and 2 are logic related. The 2 two non-graphics files are Calculations and GlobalVars, which perform the physics calculations and store global variables respectively.
The program starts in App.kt, which calls Inputs, OutputNumbers, and CollisionGraphics:
- Inputs takes the user inputs for velocities and masses, has Calculations calculate final velocities, and stores them in GlobalVars.
- OutputNumbers displays the final velocities to the user.
- CollisionGraphics is the most complex and contains the graphics for the two blocks colliding.
I decided to use the method based on the centre of mass, rather than with conservation of energy. This is because it's far easier to implement, requires fewer calculations, and allows me to deduplicate code - leading to better performance in the final project.
To build and run the development version of the desktop app, use the run configuration from the run widget in your IDE’s toolbar or run it directly from the terminal:
- on macOS/Linux
./gradlew :composeApp:run
- on Windows
.\gradlew.bat :composeApp:run
*I recommended using java 24, other versions have not been tested