all side questssource
quadtree collision detection
hundreds of circles bouncing around. checking every circle against every other one gets slow fast, so a quadtree splits the box into smaller and smaller squares and each circle only checks its neighbours. touching circles light up.
how it works
every frame, each circle is inserted into the tree. a square holds up to 10 circles; once it gets an 11th, it splits into four smaller squares and hands its circles down. busy areas end up with lots of tiny squares, empty areas stay as big ones.
to find collisions, each circle only looks at the circles sharing its squares instead of all of them. the numbers under the demo show how many checks that saves compared to the brute force way.