I recently unearthed an old Java applet that I had written many years ago and decided to see what it would look like as a single page canvas application (an HTML5 compatible browser is required to view this page). The applet was inspired by the way blackbirds roost on power lines in San Francisco (and probably everywhere else). I started with something close to Craig Reynold’s classic Boids algorithm, which is remarkable for generating motion resembling a flock of birds by giving each particle only 3 relatively simple mathematical constraints. I had to add 6 more rules to incorporate the power line behavior (2 for landing, 1 for hopping on the line, and 3 for launching off the line).
The rules are:
A flying bird is attracted to the average location of all nearby birds.
The velocity of a flying bird will tend towards the average velocity of its neighbors.
A bird will fly in the opposite direction of any object (another bird or a wall) that is too close.
A flying bird within a certain distance of a power line will slow down.
A flying bird within an even smaller distance of a power line and flying a low velocity will land.
A sitting bird will make a series of discrete steps to either reach a comfortable distance from a neighboring bird or to equalize the distance between two birds.
A sitting bird trapped at an unacceptable distance between two birds will launch.
A sitting bird will launch if the average velocity of nearby flying birds meets a certain threshold.
A sitting bird will launch if a flying bird approaches too closely.
Feel free to view the source and use what you like. I’ve been doing a lot of canvas work that I’ll be posting about here soon.