Flocking

The sheep in the game are supposed to behave as a flock, meaning that they will tend to walk together close to each other. The flocking behaviour is achieved by calculating different vectors and combining them into one for each sheep. The three different vectors are: avoidance, group, and randomness.

Avoidance
The avoidance vector is there to make sure that the sheep do not bunch up too closely, and if it was not there then the sheep would likely try to converge on a single location. To get the avoidance vector, you get the vector between the sheep you want to avoid and the sheep you’re calculating the avoidance vector for, and invert it. In this sense, it points away from the sheep that you wish to avoid. As a sheep has to avoid multiple sheep, the avoidance vector becomes an average of these.

Group
The group vector is the direction of the flock. It is acquired by getting the current direction of each sheep and averaging them to get a single direction.

Randomness
The randomness vector is, as the name suggests, a random direction to be added to each sheep.

These three vectors are then weighted and combined into the flocking vector.