↓ Skip to main content
  1. Home projects/

Dancing with Friends and Enemies: millions of dancers on the GPU

··Index: 7·
Table of Contents

Every dancer picks one friend and one enemy. At each step dancers shrink a little toward the middle of the floor, take a big step toward their friend and a small step away from their enemy, and now and then they change their minds and pick again.

One dancer picks a friend and an enemy anywhere on the floor, then takes a big step toward the friend, a small step away from the enemy and a small step toward the middle.friendenemydancermiddle of the floorbig step toward the friendsmall step away from the enemysmall shrink to the middleno neighbourhood: each dancer reacts to two others, wherever they are
The rule for one dancer in one step

This rule comes from a 2012 post by Simon Woods on Wolfram Community. I ported it to WebGPU and scaled it up from thousands of dancers to millions.

What makes it work is that there’s no neighbourhood at all. Woods found that searching for nearest neighbours was too slow, so he let every particle react to just two others, wherever they happen to be, and the whole look of the swarm comes out of that one choice.

What I did
#

Simulation, point drawing and glow all run on the GPU, in WGSL compute and render passes. The page itself is plain ES modules, about 2,100 lines of JavaScript, with no dependencies.

You can set how many dancers there are (from 10,000 to 4 million), who picks whom (random, rings, cliques, local), 2D or 3D, step sizes, colours and trails, and you can push dancers around with the pointer. All of the state goes into the URL, so if you send someone a link, they’ll see exactly what you saw. In my benchmark with 4 million dancers the median was about 124 frames per second.

You need a browser with WebGPU: Chrome or Edge 113 and later, Safari 26 or Firefox 141.

Related