# Goldfish, Flocking algorithm

One of the most interesting projects I have taken on in the world of JavaScript was the implementation of a Flocking Algorithm. The idea is to create a simple AI brain, and apply it to a flock of agents, usually referred to as “Boids” (In my simulation they are called goldfish), so that they collectively move and behave as a flock. Each agent has only its own rules and the flock as a whole has no understanding of the flock concept. By simply having each agent move, based on a few simple concepts, the flock will naturally form and start behaving like a flock.

In a full flocking simulation there are three simple rules that each agent must follow:

• Alignment
• Cohesion
• Separation

In short, alignment means that the agent will steer to move in the same direction as its peers. Cohesion means that the agen will try to move to the enter of the group, and separation means the agent will try to avoid bumping into anyone.

In the simulation above, I have only implemented Alignment, so far. I do plan on implementing the other rules as well, I just haven’t gotten to it yet.

I will update the article here, with more information, when I get more features implemented.

Along with the flocking rules, I have a range of other ideas to implement as well, when I get the time. Those include:

• UI elements, so you can control different parameters regarding the rules (Like turning the rules on and off)
• Rocks or other obstacles that the fish must avoid
• Sharks that will follow a different set of rules (Eat all the fish) and the fish will try to avoid, while still following the rules
• Different kinds of fish. So you can have two or more flocks. Each agent will still try to avoid bumping into anyone, regardless of what flock they are from, but they will only align and cohere with their own flock
• Maybe some ‘chaos’ that will allow fish to sometimes steer in a different direction than the flock, to introduce some random movement
• Maybe put boundaries on the area, so fish has to stay inside the map (Right now it wraps around, so when they leave out one side, they immediately enter through the opposite side)