Stian Eikeland bio photo

Stian Eikeland

Developer, hacker, techno-foodie, diver, hobby-photographer. Does consultancy work from own company. Lives in Bergen, Norway.

Twitter Github Flickr Vimeo Instagram 500px

Flocking / Synchronization

There are a few behaviors in nature that seems really advanced, impressive and organic, that actually can be simulated on a computer using only a few simple rules. One of them is flocking - the "formation"-flying of birds or movement of schools of fish. There's an old algorithm/simulation by Craig Reynolds called Boids (often used in screensavers and even used for modeling the flying bats in batman) that explains this pretty well. Make a few objects that react to their neighborhood according to the following three simple rules:

  • Separation - try to stay at least x units away from all neighbours.
  • Alignment - try to match the average direction of your neighbours.
  • Cohesion - steer towards the center location of your neighbours.

Implement the rules and depending on how you weight them you quickly get behavior that looks impressively organic. Was bored the other evening, and my coding fingers are itching since I've barely gotten to do any programming at work these last months - the meeting/planning vs programming ratio is through the roof. So I did a quick implementation for shits and giggles. Check it out here: (or click the image above). I have no idea if it works with all browsers, but works with chrome at least.

There's also a decent TED talk on the subject:

What's it going to be used for? Notting really, but might be useful to know if I ever get to chance to program a large army of hunter killer robots with swarm capability.