## 1. Flocking Simulation

In this project, I implemented the flocking simulation algorithm from the paper "Flocks, Herds, and Schools: A Distributed Behavioral Model" by Craig Reynolds and added several features for interaction purpose.

## Flocking algorithm

In the flocking simulation algorithm, each simulated creature is called a 'boid' and they make their own decision of move direction by looking at all the neighboring boids. There're three kind of driving forces modeled by Craig's paper, in the course project, we added an additional wandering force that randomly disturbs the boids to create a more natural motion.

A description of the three kinds of forces can be found here, here we only give the parameters we used in the program.

The entire grid is 50*50*50 and each boid has length 1 and mass 1. The neighboring radius is 10 for flock centering force and velocity matching force and 6 for collision avoiding force.

1. Flock Centering Force

The force is modeled as F_i=Sum_[j=1:n] (1/r^2)*(P_j - P_i) / Sum_[j=1:n](1/r^2). Where i is the current boid, j is the neighbor boid, n is the number of neighbor boids, r is the distance between boid i and j, P_i and P_j are positions of two boids.

2. Velocity Matching Force

The velocity matching force is modeled as F_i = Sum_[j=1:n] (1/r^2) * (V_j-V_i) / Sum_[j=1:n](1/r^2). Where V_i and V_j are velocities of two neighboring boids.

3. Collision Avoiding Force

The collision avoiding force is modeled as F_i = Sum_[j=1:n] (1/r^3) * (P_i-P_j) / Sum_[j=1:n](1/r^3). For this force we use a smaller radius for the neighbors and use 1/r^3 to weight the force so that when boids can attract each other without colliding.

4. Wandering Force

This force is used to randomly perturbate the boids so that when all the other forces are turned off, they still look natural. Here I used the Perlin noise, so the force is F_i = (perlin(p_x, p_x, p_x), perlin(p_y, p_y, p_y), perlin(p_z, p_z, p_z)), where perlin(x, y, z) is the perlin noise function and p_x,y,z are the three components of the position vector.

With these forces computed, I linearly combines them into one final force. The weights are adjustable, while I found [4, 12.5, 6.5, 0.5] an effective set of weight. Also, a threshold is set for the forces to prevent forces from being cancelled out by each other. For each boid, a maximum and minimum velocity (4-10) is set to keep the boid from moving too fast or slow.

A description of the three kinds of forces can be found here, here we only give the parameters we used in the program.

The entire grid is 50*50*50 and each boid has length 1 and mass 1. The neighboring radius is 10 for flock centering force and velocity matching force and 6 for collision avoiding force.

1. Flock Centering Force

The force is modeled as F_i=Sum_[j=1:n] (1/r^2)*(P_j - P_i) / Sum_[j=1:n](1/r^2). Where i is the current boid, j is the neighbor boid, n is the number of neighbor boids, r is the distance between boid i and j, P_i and P_j are positions of two boids.

2. Velocity Matching Force

The velocity matching force is modeled as F_i = Sum_[j=1:n] (1/r^2) * (V_j-V_i) / Sum_[j=1:n](1/r^2). Where V_i and V_j are velocities of two neighboring boids.

3. Collision Avoiding Force

The collision avoiding force is modeled as F_i = Sum_[j=1:n] (1/r^3) * (P_i-P_j) / Sum_[j=1:n](1/r^3). For this force we use a smaller radius for the neighbors and use 1/r^3 to weight the force so that when boids can attract each other without colliding.

4. Wandering Force

This force is used to randomly perturbate the boids so that when all the other forces are turned off, they still look natural. Here I used the Perlin noise, so the force is F_i = (perlin(p_x, p_x, p_x), perlin(p_y, p_y, p_y), perlin(p_z, p_z, p_z)), where perlin(x, y, z) is the perlin noise function and p_x,y,z are the three components of the position vector.

With these forces computed, I linearly combines them into one final force. The weights are adjustable, while I found [4, 12.5, 6.5, 0.5] an effective set of weight. Also, a threshold is set for the forces to prevent forces from being cancelled out by each other. For each boid, a maximum and minimum velocity (4-10) is set to keep the boid from moving too fast or slow.

## Additional Features

Apart from the standard flocking simulation algorithm, I also added several features to the program for fun. A sphere is created that can either attract or repulse the boids and user can control it by mouse. Also, I used a cubemapped box as background. For each boids, I created a fish model that swings its tail randomly in a natural fashion. Lastly, user can switch to the fish view mode and watch from the point view of one of the fishes which created a pretty cool experience.