Boids AS3 Demo

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.

Posted on 05 May 2009
37 Comments
8 Trackbacks

Meta

Flipping you the Boid was posted on May 5th 2009 in the category Lab / Actionscript 3.0, Code, Flash, Lab, Open Source and tagged; , , , , , , , .

You can Leave a comment.

Twitter <follow>

March 10th 2010 - 8:37am

Roll on the SDK! Unlimited Detail Technology http://youtu.be/Q-ATtrImCx4?a (via @somerandomdude)

Discussion

45 Responses to Flipping you the Boid

Leave a Reply

Pingbacks / Trackbacks

  1. 10 months ago AS3 Boid Flocking Simulation using Steering Behaviors - AI Flash Player 10 — Some Random Dude

    [...] AS3 Boid Flocking Simulation using Steering Behaviors – AI Flash Player 10 [...]

  2. 8 months ago Notes & quotes » Blog Archive » Mother flocker

    [...] the day after I wrote those previous lines, I had an email from Justin Windle tellin me about is brand new flocking class  for FP10. I was very exited because it was he most achieved boid manager done in as3 (at least for [...]

  3. 7 months ago Notes & quotes » Blog Archive » Flocking manager for away3D

    [...] I rewrote and was gonna work again on it. Then a few days ago Justin came with his wonderfull boid class, that was my new start point. I had to rewrite a lot of it to make it accurate with away3D, because [...]

  4. 7 months ago Flocking around with AS3 and Multitouch | SethSandler

    [...] recently came across a great and clean example of some boids code by soulwire (make sure to check out his whole site; it’s great). Since we’ve seen various examples [...]

  5. 4 months ago Overarching Inspiration < egnm3004

    [...] Soul Wire & Flock Systems Post a comment | Trackback URI [...]

  6. 4 months ago The secret life of points (part 1)

    [...] A computer science classic is Craig Reynolds’ Boids, a system where a few points each with a set of simple behavioral rules and environmental awareness can approximate bird flocking in a scarily accurate way. An excellent Flash example can be found here. [...]

  7. 1 month ago alberne frage... - Flashforum

    [...] pros – sch

  1. Sharedtut 1 month ago

    Great article, thank you for sharing.

    Reply to this comment