By Chris Hoynck van Papendrecht 15340791
This program simulates the flocking behavior of boids (artificial agents) in a 2D environment. The simulation demonstrates how simple rules—cohesion, alignment, and separation—can result in complex group behavior resembling bird flocks. The boids also avoid boundaries using a repulsion mechanism.
-
Flocking Rules:
- Cohesion: Boids steer toward the midpoint of their neighbors.
- Alignment: Boids align their velocity with nearby neighbors.
- Separation: Boids avoid getting too close to others or the border.
-
Boundary Handling:
- Boids bounce off the edges of the simulation space.
-
Visualization:
- Boids are visualized as moving points in a 2D plot.
-
Customizable Parameters:
- Number of boids in the flock.
- Maximum speed and boundary repulsion strength.
-
Ensure you have the required libraries installed:
pip install numpy matplotlib scipy
-
Save the program to a Python file, e.g.,
boid.py. -
Run the program:
python boid.py
-
Behavior Formation:
- The boids take a few seconds of simulation to organize and demonstrate clear flocking behavior.
-
Parameters:
- You can adjust the number of boids by changing (change 100 to desired number of boids):
flock = Flock(100)
- Modify the flocking and boundary rules by tweaking weights in the
apply_rules()method.
- You can adjust the number of boids by changing (change 100 to desired number of boids):
- Represents an individual boid with:
- Position
- Velocity
- Acceleration
- Manages the flock of boids.
- Implements rules for movement and interactions:
- Cohesion, alignment, separation, and border repulsion.
- Updates positions and ensures boids remain within bounds.
- Real-time animation using
matplotlib.animation.FuncAnimation.