Project 2

hockey-playing characters

Q & A:

  1. Q: Shiram Iyer: Since you have alignment rule implemented, isn't it straightforward to just set the orientation of the boids to the direction of the resultant velocity vector? Why did you choose to give the boids a random orientation? - Shriram Iyer

    A: The orientation is not random. I do not realign each boids coordinate system when they turn, but I instead tried to use vector math to rotate them towards their velocity vector within coordinates of the paper. There was an error in my math:) It was a simple fix. [see next question]


  2. Q: I noticed that you had a problem aligning the letter's direction with the letter's goal. Did you try to calculate ideal rotation and [...] figure out how much further it needs to turn to meet the goal?

    A: It's simpler than that. There is a vector that represents a letter's original orientation...vector A. Say, vector B represents the direction that a letter is moving in. All that needs to be done is to rotate A towards B and then update A with resulting orientation. It is easy to figure out the angle between A and B, but it's not straight forward which way to rotate. This was the error in my clculations. In order to figure that out we need to do find the cross product of A and B and look at the Z component of the resulting vector. If it's negative then rotate to the right and if it's positive then rotate to the left. My error was looking at the wrong component of the cross product of A and B.

  3. Q: Alessandro Febretti:
    1 - Can you explain better in which case the pack is succesfully passed between teammates and when it is intercepted by an opponent?

    2 - I quite liked the bouncy effect you implemented on line obstacles. The interesting thing is that the obstacles' bounce center is exactly in the point they've been hit by a boid. How did you do that, do you split every segment in several pieces when you create it, or you do the splitting dynamically when the obstacle is actually hit?

    A: If there are any teammates around, a boid will pass the puck to other teammates if it is approached by an opponent. Just to have some randomness, 1/10th of a time it will lose the puck even if it can pass it.

    The bouncy effect is exactly what it looks like - the line segment is split dynamically at the point of interesection of a boid's direction bector and the line. Then I use the cosine function to create a little bouncy animation for the point where the line splits.

Snapshot:

Download: osx binary (intel) source code
To execute: ./alphaboids [world text file]

Boids:

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

  1. There is one puck and one goal.
  2. Boids can push each other off the piece of paper.
  3. Multiple flocks can be a part of one team.
  4. Focks can collide and bounce off each other.
  5. A team of boids tries to obtain a moving puck and throw it into the goal.
  6. 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.
  7. If a boid is close proximity to the goal, it will shoot the puck towards it.
  8. Once the puck is in the goal, it is re-entered into the game. It moves around freely until picked up by a boid.
  9. 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:
Objects that can be placed into the scene are the following:
  1. Arbitrary number of teams of boids (teams that could be composed of different characters, but only same characters will try to flock).
  2. Obstacles composed of arbitrary line segments.
  3. Sticky obstacles that boids get sucked into.
  4. Obstacles that are stationary segments of text that boids will go around.

Implementation:

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.

Controls:

Z - roll
X - pitch
C - yaw
MOUSE - zoom in, pan around