[Behaviour] Boids Flocking

1 favourites
  • (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!]

  • Wow. Very cool.

  • Great plugin. we get very few game play plugins and its refreshing to see something useful. This will diffidently get used in my projects. Thanks and hope you continue doing game play related plugins! 8)

  • Looks good, fps is solid 60.

    One thing, the action allows you to select other objects.

    I don't see the purpose here as a behavior would automatically be applied to all instances only.

    Other than that its a great implementation.

  • Thanks all for the feedback.

    newt - I think I had originally planned to make it possible to flock with instances of other types of object but, as you point out, it probably makes more sense to just flock all instances of the same type of object on which the behaviour is added.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Very interesting! Good for npc AI.

  • This is great! Thank you tanoshimi I just requested this in another thread as a suggestion for Construct 2 updates. it is on page 2 if you are interested :)

    http://www.scirra.com/forum/topic62646_post387830.html#387830

    I describe an "alpha" system for promotion.

    The system was very useful for situations where they wanted troops to follow the next in command, or if they wanted the Strongest, or the boid with the most health to seem to take the lead as the alpha.

    Please excuse me if you have already added something like that.

    I am looking forward to getting off work, and trying this in my spare time 8)

    Thanks again tanoshimi :)

    Edit:

    A way to choose 2 or more animations to randomize would be really neat too. That way they would not all be walking exactly the same way, some can have a limp, some can have injuries, or a change in armor, or clothes. That would all be possible with some sort of animation randomize-er. I understand it would be really easy to just do this with the current on create block+object set animation random (X,Z) . I am just suggesting it to streamline production usage :)

  • This is awesome! Thanks.

  • Any chance of some updates like setting max speed at run time, and an activated setting?

  • It sounds awesome but I am forbidden?

    Best Regards;

  • Excellent stuff!

    Only thing is that when mouse stops and flock catchup... they go nuts!

    If you can update that flock goes to circular or "run in eights" mode that would be just perfect!

    Or even better... that we can chose between stop mode or circular or "eight" mode...

    Becouse this can also simulate soldiers in an RTS just as easy as bats in the sky.

  • Becouse this can also simulate soldiers in an RTS just as easy as bats in the sky.

    This was exactly my thought after seeing the first post.

    Soldiers, tanks the list could go on..

    Thanks

  • tanoshimi

    this is great !

    thanks !

    but i'm thinking in a way to allow the sprite to avoid some obstacle like a wall for example.

    would you have an idea ?

  • manukeo Try rexrainbow's Pushout Plugin

  • Welcome!!!!!

    very excellent!!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)