---
title: "Dancing with Friends and Enemies: millions of dancers on the GPU"
description: "A swarm simulation on WebGPU where every dancer follows one friend and avoids one enemy, up to four million dancers in the browser."
date: 2026-08-12T12:00:00+02:00
lastmod: 2026-10-16T18:58:52+02:00
url: https://klimek.link/projects/dancing-with-friends/
author: "Marcin Klimek"
source: https://klimek.link/projects/dancing-with-friends/
---

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

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.

*Diagram: 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.

Source: https://klimek.link/projects/dancing-with-friends/
