You are a trashcan with a delicious stench. All of the flies want to be near you.
This demo is an implementation of the famous Boids algorithm, as described by Conrad Parker. One of the great things about Boids is that you get a fun, emergent herding behavior from three simple rules.
There are three main forces acting on the flies (and one bonus):
- Cohesion - the tendency of each fly to move towards the average of the group
- Separation - the tendency of each fly to move away from any fly close to it
- Alignment - the tendency of each fly to adjust it’s velocity to the average of the group.
This code also implements a 4th dynamic:
- A goal - each fly will also tend to move towards the goal: you! the trash can.
This is awesome!!! I’ve been wanting to bring boids to arcade… but I am glad I waited!
Would you be interested in packaging it into an extension?
Do you have any thoughts on the scope (and even signatures) of the API?
I was thinking that it might make sense to have some sort of
Flock object that you could add sprites to.
In creating this, one of the things I was exploring this was making an “agents” API that has things like:
followSpriteOfType <SpriteType> (we could make pets that follow you around!)
ifSprite <Sprite> isFacingHeading <HeadingEnum = N | S | E | W | NW | ... etc> (for setting directional animation frames)
ifCanSeeSpriteOfType <SpriteType> (enemies that attack if they see you, considering walls)
We probably wouldn’t put all of these ideas into one extension. But I’m just trying to think through the primitives that make let others compose more interesting behaviors.
You need an api to find neighbors within the cone of vision.
Then you can define high level behaviors that work together: follow the leader — or avoid the predator — are behaviors that get combined with the general boids 3 rule set.
The key to perf is finding neighbors efficiently.