Wednesday, May 30, 2007

Flocking Birds - A Boids Deviation

This is my second post in a series covering some popular AI topics. Previously I discussed emergent behavior and the game of life. In this post I continue with a slightly more advanced and practical example, Boids. I finish by showing an example and how I modified the classic Boids approach just a little.

Boids is a set of 3 simple rules. When these rules are applied to many agents (birds in this case) they behave like flocks of birds. Agents group together to form flocks and fly together as one. Flocks can merge into one or divide into multiple new flocks. These set of rules can be added to and modified fairly easily and applied to other types of creatures to achieve a wide range of behaviors.

Here is the classic Boids rule set. Each agent (boid) follows these rules:
  • Separation - If the agent is too close to another agent, then steer away.
  • Alignment - Steer toward the average heading of nearest boids
  • Cohesion - Attempt to match the speed of the nearest boids
If you think about the above rules you can visualize how flocking will occur. In my example below I added a fourth rule:
  • Avoidance - If too close a non-boid object (such as a tree), then steer away.

In addition to the 4th rule I added a restriction. An agent is only affected by an agent that it can see. It can see an agent if it is within a certain distance of that agent, and if the cone of view is such that it can actually see that agent. So, the leader of a flock of birds is not affected at all by its followers because from its perspective they aren't even there. What this gives us though is a a flock that mostly follows the leaders. If you give a minor perturbation to the a leader then you can affect the entire flock!

Enough rambling. Check out the example below. Download the source if you're interested.

One last note: In my example you might notice the birds rotating a little funny sometimes (wobbling or spinning). The movement is right, the rotation needs a little love. Right now they rotate to face the instantaneous direction of movement. In reality a bird won't rotate directly to the right if its going to slide a bit to the right while in flight (wow that rhymes).


3 comments:

eurasianbro said...

Hi I really enjoyed your creation, but for some reason my flash 8 cant open the .fla file.. do I need CS3 to open it?

eurasianbro said...

..or is there a way I can make my own version of this idea by having the actionscript in the .fla frames alone? (I understand that you put the AS in separate docs for efficency but..)

seth said...

this stuff is fascinating! well done. i'm sifting through your code now, seeing if i can add a lot more boids and seeing how that affects system performance.