I’ve uploaded the Boid source code. Sorry for the delay. I’ve also included some basic examples of how you can create nice behaviors using the Boid class. If you set Main.as as your document class then you’ll see that within that you can specify the demo to run when you compile.
The basic demos included are:
- Chase – Boids chase each other
- Flock – Boids flock together with some wander
- Graphics – How to use custom graphics with a Boid instance
- Seek – Boids seek the mouse position
- Wander – You guessed it, they wander
I’ve commented all of the methods and properties and there is also documentation (open index.html in the docs folder) to get you started. Before you mention it, yes, there is some redundant code ;) (such as the constrainToRect method which doesn’t really work) but I need to do some optimisations anyway so I just left it as is for now, rough and ready.
Download: AS3 Boid Class & Demos
It’s been a little while since I posted! I’m in the process of moving house and office, plus I’ve been repeatedly slammed at work. Apologies to those of you who’ve left comments and haven’t got much by way of reply. I’ll hopefully be visible again over the next few weeks.
I actually coded this experiment back in November, but only recently decided that enough was enough and hijacked an evening so I could tidy up the code and build a UI for the demo.
Since coming across Craig Reynold’s paper; “Steering Behaviors for Autonomous Characters” whilst I was researching complexity theory for a university project (it wasn’t as academic as it sounds!), I’ve found myself becoming fascinated by flocking and swarm beahviors and the notion that you can create complex systems from relatively simple components. Much like the Cellular Automata, you can program an agent to have only a limited set of abilities and awareness, define a set rules for it to follow, and then create a multitude of instances of the agent to see what patterns begin to emerge through their interactions.
Flocking is a great example of this, as Reynolds demonstrated. His agents, or “Boids”, could perform certain maneuvers, such as seeking a target, fleeing from a predator, avoiding obstacles or wandering randomly. Combine these behaviors though, and allow each Boid to consider it’s neighbors when deciding on its locomotion, and complex behaviors begin to emerge.
The most infamous of these combinations is that of separation, cohesion and alignment, as it can produce a swarm behavior reminiscent of flocking birds or shoals of fish.
Separation means that each Boid attempts to maintain a certain distance from its immediate neighbors, Cohesion ensures that each Boid attempts to stay close to the centre of the immediate flock and Alignment will calculate the average heading of the flock and steer towards this vector.
Each of these behaviors is fairly simple in isolation, but the results can give the impression that some significant AI is at work, when all that’s really going on is some simple vector math.
Robert Hodgin has done some amazing work with flocking behaviors, so too has Psyop, and of course Reynolds is the daddy of all this stuff. There are some good libraries out there too, such as OpenSteer, and Keith Peters talks about this too in Advanced Actionscript Animation.
I’ve been trying to sharpen up my mathematical skills lately though, and was eager to play around with the new (new when I created this experiment anyway!) Flash Player 10 API – the 3D aspects in particular. So at the core of the Boid class are the native Vector3D, Matrix3D and PerspectiveProjection classes. Ok, so we’ve all got our own Vector3D and Matrix3D class in our library, but I thought it’d be good to use Adobe’s, especially seeing you can then apply the Matrix3D directly to a DisplayObject’s transform property.
Another cool feature I came across was the Matrix3D.pointAt() method, which will rotate a Matrix around a given axis and in this case can be used to orientate the Boid to it’s heading vector… Nice!
I’ve had a lot of fun putting together the Boid demo anyway – tweaking various settings on the individual Boids as well as the group behaviors (my personal favorite is ‘Chase’ mode with ‘Number of Boids’ cranked right up!).
If you have a play with the settings though, you’ll quickly see what I mean about the plethora of different effects that can be accomplished, even by simply adjusting the locomotive abilities of the Boids.
Due to said lack of free time as of late, I haven’t documented the code fully, though I have started and so if anyone is interested in the source code then please say so in the comments and I’ll try and finish the job (after a mere 6 months!) and put it up for download.

Great work !
I did implement a boid manager for 3D part such as away3D and papervision:
http://www.agence-anonyme.com/blog/?p=147
but lost the classes in a plane crash,
have to write it back…
Yes, the code would be magnificent! I am so curious…
I’d love to see the code for this. I’ve developed flocking code, but just flocking, I like the chase part in particular ;)
Boid source code is now available :)
Thanks for releasing the code! I have yet to check it out, but I appreciate it.
Hey, this is really cool… am currently trying to program a very simple game based on flocking behaviour, this is a great example. How on earth does yours run so fast though? I’m using Processing, which is basically Java and should be noticeable faster than actionscript (at least thats what i understood), but my program struggles over 200boids (my computer’s pretty old) in 2D while yours flies with 250 in 3d!
Hey Justin,
I’m really impressed with this! I’m currently working on a game, using some of your algorithms (except in Away3D instead of Adobe3D) and wondering how i can do a few things. First, how can I confine the flock to a sphere? Is this possible without using collision detection?
Great stuff, brilliant seeing the beauty of math….
Nice effect. I am surprise how smooth each particles can move even at 250 of them!
Nice effect. I am surprise how smooth each particles can move even at 250 of them!
Hi guys. I’m beginner in programming AS3 but I can do some things. I just need to know if it is possible to change the triangles (shape) of MovieClip and it will change the very structure of the classes? And if someone has a tip to do this without much complication. I appreciate the attention. Thank you!
Amazing! This particular demo have spurred an interest in boids and complex behaviors for me and I would love to see a full documentation to be able to use it a reference. Good Job…loving all the work you have done so far.