Collisions is a simple elastic collisions simulator of two spheres in a 2D space.
It calculates the velocities and directions of the spheres after collisions, using linear algebra and avoiding the use of complex trigonometry.
The vector operations needed to calculate the elastic collision are simply outlined in the doc 2-Dimensional Elastic Collisions without Trigonometry written by @vobarian.
I have not imported any external library for the vector operations, I wrote vectors.go with few basic functions.
Collisions is made of four packages:
- wren: it contains the core function to calculate the speed of the spheres after the impact.
- detector: it contains the functions to detect the collision of the sphere, with each other, or with the edges.
- shapes2D: it contains the function to draw the circles (the spheres).
- vectors: it contains the basic functions to work with vectors.
I used Ebiten - a simple graphics library to build games in Go.
go run main.go
A while ago I wrote something similar in Java, but I wasn't happy with the result and I did not share it.
Now, I am learning Go, and I decided to resume that old idea to write my Hello Word in Go.
Some ideas are:
- Use any number of spheres
- Add friction
- Add gravity
- Add inelastic collision
- ...
Feel free to clone this project and expand it at your whish.