Skip to content

Learning how to create an flocking simulation...with fish!

Notifications You must be signed in to change notification settings

SirLorrence/flocking-prototype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flocking Prototype

With this project I took my time to learn how to do an simple flocking simulation. My knowledge of this is still elementary, but I'm planning on implementing this as an game mechanic in one of my up coming projects. For now...look at the little fishies.

Source Code

The Rules

1. Move towards the average position of the flock - Cohesion

This is done by each entity (Craig Reynolds calls them boids) calculating the other entities position and creating an average center position Getting the other's entities positions -

centerVector += neighbor.transform.position;

After dividing the sum of the positions by the size of the flock -

centerVector /= flockSize;

2. Align with forward heading of the flock - Alignment

This is done by adding all the Forward Vectors (facing directions) and dividing them by the size of the flock. Since the entities are just going forward I used the same centerVector variable to calculate the foward direction:\

var desiredFacingDirection = (centerVector + avoidanceVector) - transform.position;

3. Avoid crowding together - Separation

When another entity is too close to one another, you'll need to calculate a new forward direction by using the flock forward heading (vector) + the direction of the where to avoid the other entity + the flocks position.
Getting the avoidance direction vector: \

if (closetNeighbor < _manager.NeighborDist) avoidanceVector += transform.position - neighbor.transform.position;