This adds a new movement behaviour based on the "Boids" flocking model first described by Craig Reynolds at http://www.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 http://www.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.
Behaviour is http://www.a3uk.com/boids/boids_behaviour.zip, and example CAPX is http://www.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).
[p.s. I must congratulate the Construct2 designers for creating such an elegant plugin system - it was refreshingly simple to use!]