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.
Each individual modifies its behavior due to three neighbors observation:
leader – closest individual in front of it,
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.
Simulation steps: For each individual X, execute following steps:
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)
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.
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.
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.
Avoid collision with cursor in the same way, separation rule avoided collisions with birds.
If the velocity exceeds maximum speed or minimum speed, rescale the velocity vector.
Reposition bird X based on its calculated velocity vector.
Restarts the simulation. Initial positions of birds are randomized.
Pauses/Starts the simulation.
Next frame (+)
In case of paused simulation, executes next step, and redraws canvas.
Number of birds
Number of birds in the flock.
Set birds speed limits.
Steps per frame
Increases number of simulation steps per each drawed frame.
Turns separation rule on/off.
Distance threshold from which bird starts to change flight direction.
Rotation that occurs once the bird approaches an obstacle.
Turns alignment rule on/off.
The influence of neighbor directions on bird flight direction.
Turns cohesion rule on/off.
The influence of neighbor position on bird velocity vector.
Turns escaping from cursor on/off.
Distance threshold from the cursor, from which the bird starts to change flight direction.
Cursor collision rotation
Rotation that occurs once the bird approaches a cursor.