Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 2.11 KB

README.md

File metadata and controls

51 lines (41 loc) · 2.11 KB

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;