Home▸Algorithms & AI▸Barnes-Hut N-Body Gravity Approximation (2D)

🌌 Barnes-Hut N-Body Gravity Approximation (2D)

2D Canvas simulation of the Barnes-Hut N-body gravity approximation: a live quadtree, adjustable theta opening-angle criterion, and a real-time counter comparing approximate force calculations against brute-force O(n^2).

Algorithms & AI2DModerate60 FPS📱 Mobile-adapted⇄ 3D version
2d-barnes-hut ↗ Open standalone
⚙ Under the hood

Watch a quadtree approximate gravity between hundreds of moving bodies in real time: each cell aggregates a total mass and center of mass, and the theta opening-angle criterion decides whether a distant cluster of bodies is treated as one point mass or the algorithm recurses into it. A live counter compares the resulting O(n log n) force calculations against the brute-force O(n squared) count it replaces.

Barnes-HutN-body simulationquadtreegravityalgorithmforce approximation

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)