Particle Simulation

I created a particle simulator in python using OpenGL for the visualization. I included some physics like viscosity and gravity which made it possible to see some cool behaviour. As a way to interact with the whole system I could "accelerate" the box with the arrow keys. Also I could launch a particle into the box of any size using the virtual catapult. 

I was heavily inspired to do try this out after watching the video: Building Collision Simulations by Reducible on YouTube.

Collision Detection

The crux of the whole thing that makes or breaks the simulation speed is the collision detection algorithm. There are a few algorithms that I tried with some better than the others. The gist would be that lesser the collision checks an algorithm makes, the faster it will be. But some algorithms apply a computationally cheaper collision check to prune the number of candidate collision pairs the more computationally expensive check needs to do.

"A bounding volume hierarchy (BVH) is a tree structure on a set of geometric objects. All geometric objects, that form the leaf nodes of the tree, are wrapped in bounding volumes. These nodes are then grouped as small sets and enclosed within larger bounding volumes. These, in turn, are also grouped and enclosed within other larger bounding volumes in a recursive fashion, eventually resulting in a tree structure with a single bounding volume at the top of the tree."

Gravity

I just had to add integrate the position considering gravity as the only force causing an acceleration on a particle (excluding collisions of course). Now the unfortunate thing is that unlike collision which don't happen between every particle, gravity is not so forgiving. It acts between each and every particle and the complexity grows quadratically just as the brute force collision check case. Of course one optimization would be to neglect gravity between particles that are more than a certain distance away - exploiting the inverse square law.

As Newton famously said,

To which Einstein famously said,