# Group behaviors: flocks of birds

### General description

The main goal of this program is to demonstrate group behavior of the individuals. Each individual follows simple, local range rules without knowing anything about behavior of the group as a whole. It effects on behavior of whole group.

Fig. 1. Flock

Each bird follows three simple rules:

• Separation - Steer to avoid crowding local flockmates.
• Alignment - Steer towards the average heading of local flockmates.
• Cohesion - Steer to move toward the average position (center of mass) of local flockmates.

### Symulation rules

Each individual modifies its behavior due to three neighbors observation:

• left neighbor,
• leader – closest individual in front of it,
• right neighbor.

Fig. 2. Bird neighbors

If an individual doesn't have neighbors it becomes group leader and keeps given flight direction. This way group as a whole has always some direction and we can observe individuals behavior inside the group. This establishment results straight from nature observation – leading individuals follow some additional stimuli which give them information what flight direction should they keep. Information about these is not necessary for group behavior simulation.

### Implementation

Simulation steps:
For each individual X, execute following steps:

1. Find closes neighbors from bird X. Leader is chosen from set of bird placed in front of X - in the cone (-45deg, 45deg) relative to velocity vector. Left - in the cone (-135deg, -45deg), and right - in the cone of (45deg, 135deg)
2. Apply alignment rule. Calculate average directional vector based on all neighbors. Move bird's velocity vector to point closer to calculated average, so his speed won't change.
3. Apply cohesion rule. Calculate weight center of neighbor positions. Calculate the difference vector between target center and current position. Add rescaled vector to current bird velocity (new_v = (1 - wsp) * old_v + wsp * diff). This way, the individual can move in any direction.
4. Apply separation rule. Find closest of neighbors. If the distance is smaller than given threshold, steer into a direction opposite to position of that neighbor.
5. Avoid collision with cursor in the same way, separation rule avoided collisions with birds.
6. If the velocity exceeds maximum speed or minimum speed, rescale the velocity vector.
7. Reposition bird X based on its calculated velocity vector.

### Program options

Control
Restart Restarts the simulation. Initial positions of birds are randomized.
Pause/Start Pauses/Starts the simulation.
Next frame (+) In case of paused simulation, executes next step, and redraws canvas.
Basic options
Number of birds Number of birds in the flock.
Maximum/minimum speed. Set birds speed limits.
Steps per frame Increases number of simulation steps per each drawed frame.