To execute: ./alphaboids [world text file]
After implementing the basic boids algorithm in just two dimensions, I wanted to give them some meaningful but not completely random movement. The sliding movement of boids on the piece of paper made me think of air hockey. So, the boids in my program are attempting to play a crude game of hockey with one puck and one goal.
Game rules:
Neighbors of a single boid can be classified as:
teammates - boids of the same team
flockmates - boids of the same character
opponents - boids of the opposite team
- There is one puck and one goal.
- Boids can push each other off the piece of paper.
- Multiple flocks can be a part of one team.
- Focks can collide and bounce off each other.
- A team of boids tries to obtain a moving puck and throw it into the goal.
- The boid in posession of the puck and its flock will get competition from other teams. Boid will lose the puck if it is approached by opponent and it can't pass the puck to any teammate. Just to make things more interesting, 1/10th of a time the puck will be lost even if there is a teammate around. The strategy of puck passing takes advantage of the fact that flocks move aorund together.
- If a boid is close proximity to the goal, it will shoot the puck towards it.
- Once the puck is in the goal, it is re-entered into the game. It moves around freely until picked up by a boid.
- There are obstacles in the game that boids either bounce off, stick to, or just avoid / go through.
Initial conditions for the hockey game are loaded from text.
Individual boid goals / contributions can be organized like this:
Always
- Flock with boid of same character
- Collide with boids of other flocks
- Avoid / collide / stick to obstacles
- If puck is in the goal move towards center of the notepad
Boid has the puck
- Move towards the goal
Boid does not have the puck
- Move towards the boid that has it
- battle it out from opponent or help teammate move it to the goal
- Move towards the puck if no one has it
Objects that can be placed into the scene are the following:
- Arbitrary number of teams of boids (teams that could be composed of different characters, but only same characters will try to flock).
- Obstacles composed of arbitrary line segments.
- Sticky obstacles that boids get sucked into.
- Obstacles that are stationary segments of text that boids will go around.
This program was written using OpenGL and C/C++ and FLTK for windowing.
All text is rendered using the FreeType2 library and texture mapping TrueType fonts onto quads. The notepad paper is a white plane with lines drawn on it.
Collision with objects / obstacles
Sticky things
Boids can get stuck to an object. It can get out only with help of another flockmate.
Other text
Boids avoid other text on the piece of paper. A steering vector is computed based on the current boid velocity and the way a boid should move to avoid collision with it.
Line Segments
Arbitrary objects can be compolsed by specifying line segments in the scenes.
The algorithm to do collision classification with a line segments does the following:
2 points specifying a line segment
1 direction vector / ray
then:
Calculate the normal vector of the line segment and intersection of ray with line segment.
Now we can calculate the vector that would represent reflection / bouncing off the line segment.
and
The magnitude of the reflection vector is based on the current boid velocity and is biased accordingly with other things going on in the game.
Neighborhood searches
The space that boids can move around is subdivided into a regular grid with bins that maintain a list of all boids currently present at a bin on the grid. The neighborhood seach basically involves looking at the surrounding bins, getting a list of neighboring boids from these bins, and examining how far away they are.
Z - roll
X - pitch
C - yaw
MOUSE - zoom in, pan around