Mono Lake alkali flies in JavaScript

We recently went on a fantastic family camping trip to California, travelling round various National and State Parks. One of the places we saw was Mono Lake. There are lots of interesting aspects to Mono Lake, but this post concentrates on one of them: the vast numbers of alkali flies which live on the waterline.

This video (by YouTube user Jeff Lowe) gives a good impression of their behaviour, and of the beautiful scenery which they inhabit:

Walking along, your feet set off ‘waves’ of jumping flies, interrupting the flies’ normal meandering. I wondered whether it would be possible to create a simulation of the flies which captured some of this behaviour.

In a modern browser, there should be a small swarm of flies wandering around aimlessly in this square:

 

Number of flies: 100

If you click in the swarm, nearby flies jump away from the mouse click, with flies closer to the click jumping more quickly. The result is that the flies land approximately on a circle, and they then resume wandering. If you do a series of clicks in quick succession while moving the mouse along, you get something of the effect of the real flies.

Computers are quite quick these days

The CPU in my desktop PC achieves about 75% of the benchmark score of the slowest of the 10 most popular current CPUs, according to cpubenchmark.net, and it can comfortably simulate 10,000 flies at 60fps.

Future work

The ‘square’ wraps round, such that flies which wander or jump off the right re-appear on the left, and similarly for the other edges. The toroidal nature of the flies’ universe is not perfect: the ‘distance to mouse-click’ calculation does not take account of this. It should.

It would be cool to implement some response to moving the mouse while the button is held down. Approximating the mouse movement as a series of straight line segments would be reasonable and not too tricky to do.