(Disclaimer - I've only been working with Construct2 since Monday, so I have no idea if this follows best practices or not, but it seems to work for my purposes so I thought I'd share it here!)
This adds a new movement behaviour based on the "Boids" flocking model first described by Craig Reynolds at cs.toronto.edu/~dt/siggraph97-course/cwr87
When flocking, a character's movement is based not only on its desire to reach a target, but upon the relative position and speed of other members of its flock. There are four factors that influence the direction of movement - the desire to:
- Head towards a target position
- Avoid collisions with immediate neighbours
- Head in the same direction as the rest of the flock
- Stay close to the centre of the flock
I had tried originally to implement this behaviour using the "Custom Movement" behaviour, but it seemed to involve a lot of hacking on the event sheet (explored by R0j0hound in scirra.com/forum/boids-tests_topic59833.html), so wrapping it in a new behaviour seemed more appropriate.
Using the attached "boids" behaviour, you can set the target X,Y and also specify an object type to which an instance will flock together. The relative influence of each factor on the final direction of movement is governed by the targetPriority, seperationPriority, alignmentPriority, and cohesivePriority parameters. The seperationDistance parameter controls the optimum distance which each boid tries to maintain from others in the flock.
<img src="http://i50.tinypic.com/102t56d.jpg" border="0" />
Behaviour is a3uk.com/boids/boids_behaviour.zip, and example CAPX is a3uk.com/boids/Boids.capx. The example (which obviously requires the behaviour to be installed) shows 10 bat sprites that will follow the mouse cursor around the screen. You should notice that the movement of each individual bat is affected by the behaviour of other bats in the flock and, while they aim towards the cursor, they should also steer to avoid collisions (seperation), and try to remain together (cohesion).
<img src="http://i49.tinypic.com/5bpikj.jpg" border="0" />
Comments/suggestions welcome.
[p.s. I must congratulate the Construct2 designers for creating such an elegant plugin system - it was refreshingly simple to use!]