Boids

December 11, 2023
Frank Martinez

1. Description

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.
rules

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:
cohesion

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:
align

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:
separation

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:
raycast

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: