For my final project in computer animation, I dived into Boids – a
cool simulation of flocking behavior. I made virtual creatures, or "boids,"
follow simple rules: stick together, move in the same direction, and avoid bumps.
These different are explained in more detail below. I used the Unity game engine in order
to avoid having to create the environment from scratch. All the boids were programmed from scratch
using a set of 'rules' and a little bit of randomness. The boids are able to move around the
environment and interact with each other. The boids are also able to avoid obstacles that are
placed in the environment.
2. Boid Rules
There are three main rules that the boids follow: cohesion, alignment, and separation.
These rules are explained below along with simple pseudocode and diagrams.
2.1 Cohesion
The cohesion rule makes the boids move towards the center of mass of the group. This is
done by calculating the average position of all the boids in the group and then calculating
the vector from the current boid to the center of mass. This vector is then normalized and
multiplied by the speed of the boid. This is done in order to make sure that the boids do not
move too fast.
Pseudocode:
2.2 Alignment
The alignment rule makes the boids move in the same direction as the group. This is done
by calculating the average velocity of all the boids in the group and then calculating the
vector from the current boid to the average velocity. This vector is then normalized and
multiplied by the speed of the boid. This is done in order to make sure that the boids do not
move too fast.
Pseudocode:
2.3 Separation
The separation rule makes the boids avoid bumping into each other. This is done by calculating
the distance between the current boid and all the other boids in the group. If the distance is
less than a certain threshold, then the boid will move away from the other boid. This is done
by calculating the vector from the current boid to the other boid. This vector is then normalized
and multiplied by the speed of the boid. This is done in order to make sure that the boids do not
move too fast.
Pseudocode:
3. Obstacle Avoidance
In order to make the boids avoid obstacles, I used a raycasting algorithm. This algorithm
casts a ray in the direction that the boid is moving. If the ray intersects with an obstacle,
then the boid will move away from the obstacle. This is done by calculating the vector from the
current boid to the obstacle. This vector is then normalized and multiplied by the speed of the
boid. In addition, to add randomness, the boids had a 'vision range'. This is done in order to make sure
that
the boids do not move too fast. The raycasting
pseudo algorithm is shown below.
Pseudocode:
3.1 Moving Obstacle
In addition to stationary obstacles in the environment, I implemented a 'prey' like object that would follow
boids, and the boids would need to avoid it in order to avoid getting eaten.
4. Demo
Below is a video of the boids in action. The boids are able to move around the environment
and interact with each other. The boids are also able to avoid obstacles that are placed in the
environment.
5. Presentation
6. References and Resources
For this project, I used the following resources:
J. M. Lee, S. H. Cho and R. A. Calvo, "A fast algorithm for simulation of flocking behavior," 2009
International IEEE Consumer Electronics Society's Games Innovations Conference, London, UK, 2009, pp.
186-190, doi: 10.1109/ICEGIC.2009.5293611.
Craig W. Reynolds. 1987. Flocks, herds and schools: A distributed behavioral model. SIGGRAPH Comput.
Graph.
21, 4 (July 1987), 25–34. https://doi.org/10.1145/37402.37406