Also submitted four days ago:<p><a href="http://news.ycombinator.com/item?id=2246089" rel="nofollow">http://news.ycombinator.com/item?id=2246089</a><p>although it gains very few upvotes, and no comments. More evidence that getting noticed is largely chance, or gaming.
And here's a parallel version written in Clojure that scales across your cores, <a href="https://github.com/swannodette/flocking/blob/master/src/flocking/flocking3.clj" rel="nofollow">https://github.com/swannodette/flocking/blob/master/src/floc...</a>
If you haven't seen the boids pseudocode page by Conrad Parker it's definitely worth checking out: <a href="http://www.vergenet.net/~conrad/boids/pseudocode.html" rel="nofollow">http://www.vergenet.net/~conrad/boids/pseudocode.html</a><p>Here's an implementation I wrote last month for fun: <a href="http://breefield.com/lab/flock" rel="nofollow">http://breefield.com/lab/flock</a>.
It doesn't have much bias in terms of direction (there's no wind, or patterns to follow), so there's generally one large swarm. Click-drag to create repellers, boids follow mouse.
The white center of each boid shrinks at it's velocity increases, and the inverse happens to it's red direction line. Purple specks indicate perceived center of nearby flock. The rest is just aesthetic.<p>It's always super interesting to see other implementations in action, so please share more in your comments, if you have them. I'd certainly love to see them.
I used flocking in a game project in college (getting ad-hoc starship fleets to get into ad-hoc formations) with another condition: When the basic requirements are met, each ship should also try to align with a master "fleet admiral" ship, and the group of boids under consideration was limited to only ships specifically defined as part of the fleet.<p>It kind of worked, there were issues with stragglers and making it look organic that I couldn't fix in time. If ships got too far outside the formation sometimes they would only try aligning with the leader instead of catching up first. Also, when they did get in formation, they were eerily good at matching the leader's alignment: all ships would turn in unison and kind of ruin the organic feel of the pure flocking algorithm.
See also: <a href="http://en.wikipedia.org/wiki/Swarm_behaviour" rel="nofollow">http://en.wikipedia.org/wiki/Swarm_behaviour</a><p>At a research company I worked at we used swarming concepts for controlling swarms of sensors, robots, and UAVs. We also used ant colony optimization techniques mixed with genetic algorithms to do predictive intelligence. Really powerful stuff.
Here's an implementation I wrote some years ago that includes obstacle avoidance. Clicking adds obstacles, spacebar adds boids, reloading gets new colors. This ended up looking somewhat like sperm avoiding eggs...<p><a href="http://humortree.org/projects/flock03/" rel="nofollow">http://humortree.org/projects/flock03/</a><p>(Java applet warning!)<p>The source code is here:<p><a href="https://github.com/mccutchen/humortree.org/tree/master/projects/flock03" rel="nofollow">https://github.com/mccutchen/humortree.org/tree/master/proje...</a>
Very well explained. There needs to be more of this (algorithms and applied math explained in a manner somewhat accessible and nourishing to non-mathematicians through code and visualization) on the web, and I'd love to be pointed to similar resources.
Turning on the legend and the magnification of an individual boid (by clicking "Decorate") is very helpful. When you mouse over the magnified boid, you see the vectors immediately adjust, providing a very handy visual description of what is happening in real time.
I used to use boids as an interview question for candidates who whizzed through my first few code challenges -- was interesting to discuss various collision detection options with an eye on performance.<p>Such a simple algorithm w/ such a cool result.
This is fantastic! Not just the algorithm, but the explanation, the fact that there's source code readily available, and the canvas demo. Please, do more!
I've wanted to write some exploratory algorithms for flocking for a long time now. This is just the motivation I need to start working on a silly side project.<p>Thank you, everyone in the comments who submitted source code or demos. You never know when something you upload will really brighten someone's day (even if it's written in java).
Cool. There's also a book called "Swarm Intelligence" by Kennedy/Eberhart that describes swarm behaviors very well (my favorite book).<p>For anyone interested, here is the Craig Reynolds page that describes the flocking behavior <a href="http://www.red3d.com/cwr/boids/" rel="nofollow">http://www.red3d.com/cwr/boids/</a>
glad to see you've herd of this, and are flocking to it. if you're not too flighty it's worth a peep, but you may have to duck some bugs in the code.
furthermore i think flocking should be part of everyone's schooling, and there's nothing fishy about that.