# Flocking Autonomous Agents

[CG]jupoulton
44.8K views

### Open Source Your Knowledge, Become a Contributor

In the previous chapter, the desired vector pointed straight towards a random point. However, in order to display flocking behaviour, the boids must be aware of each other.

## Rules

We will add three rules to our boids to dictate their decision-making, as defined by Craig Reynolds in his paper on flocking.

Each rule will provide the boid with a different steering force. We can then calculate the average of these forces and use it for the boid's acceleration.

💡 Any number of rules can be added, such as wind, the desire to explore randomly, etc. Each resulting force can be weighted differently for the boid's final decision.

### Separation

We find a vector which points away from nearby boids.

### Alignment

We use the average velocity of the other boids.

### Cohesion

We find a vector which points towards the average position of the other boids.

Alignment and cohesion illustrate how the individuals will "cooperate". Separation, illustrates how the individuals will "compete" for space. It is this tug-of-war between competition and cooperation which will give our system its complexity.

## Flockmates

The rules above can be implemented in such a way that every boid is aware of every other boid, creating a system with one great hive-mind. However, our boids are autonomous agents, they may only take a decision based on their local environment.

For each boid, we will calculate the distance to every other boid, then if that distance is lower than a chosen constant, we calculate the three forces above, then use the sum of the three to calculate the boid's acceleration. Each force can be weighted differently, which can fundamentally change how the boids move.

## Hands-on

You can try moving the constants around or implement the three rules differently to see how your boids react now.

Steering
function getWeights() {
//TODO !
// Recommended values: 4/8, 3/8, 1/8
return {
separation: 0/8,
alignment: 0/8,
cohesion: 0/8,
};
}
function getBoidViewDistance() {
//TODO !
// Recommended value: 50
return 0;
}
function getMaxForce() {
return 0.05;
}
function getDesiredSpeed() {
return 6;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

👁 The boids grew some eyeballs to percieve the environment with. 👁

Open Source Your Knowledge: become a Contributor and help others learn.