Flocking Demo

The flocking demo was an interactive simulation I put together for an intelligent systems module at University. It uses boids inspired component flocking to dynamically form and disturb groups of intelligent agents. It's wasn't written with performance in mind as the goal was to keep the code as simple and readable as possible ( non-programming module). 


Traditional boid based agents use three rules to calculate their desired heading: cohesion, separation, and alignment. I added two more components to allow for the simulation of prey-predator relationships.

Cohesion Component

The cohesion component is responsible for calculating the vector required to bring neutral agents together. This implementation has agents calculate a perceived centre of mass for flocks in their proximity and uses the offset as the heading vector. 

Alignment Component

The alignment component controls how the heading of a flock influences the heading of an individual agent. In the original boid simulation this alignment component had agents match the median velocity of neutral agents in their neighborhood.

This implementation removes the speed aspect of the traditional alignment component and instead focuses on neutral agents aligning heading; allowing other components to manage acceleration and deceleration.

Pursuit Component

The pursuit component controls the predation of agents against those of a lower tier. It functions by causing an agent to aggressively move towards the nearest possible prey agent.

When a predator is present within an agent's awareness radius the pursuit vector will always equal the offset of between an agent and its closest predator.

Separation Component

The separation component is responsible for preventing converging neutral agents from colliding. 

Typical models for the separation component predefine a radius for each agent, within which other agents are repelled.

This implemented supports a dynamic separation radius so that agents of a differing size may flock together with few issues. The separation component executes checks on all neutral agents within its awareness radius and applies a separation vector to the host where necessary.

Evasion Component

The evasion component simulates the avoidance of predators in ground based agents. It functions by taking the average offset between predators and the host and using this as an escape vector.

The evasion vector is weighted very highly, highly enough that it can override the three original vector causing dramatic chances in flock velocity in a very short period of time. The increased weighting also grants agents the capacity to move at maximum speed, something than cannot be achieved with three base vectors.

Merging Components

An agent's flocking target vector is the vector constructed by the combined logic of all five flocking components. Each component calculates its own vector, a representation of the best position the agent could move towards to meet the individual component’s aims.


Flocking component vectors are combined using a modified version of the traditional weighted average algorithm.

Agent Source Code

Header file for the master agent class. 


Source file for the master agent class.