Game Career Guide is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.     Get the latest Education e-news

 Home Features Book Excerpt: AI for Game Developers
• # Book Excerpt: AI for Game Developers

[09.29.06]
- Glenn Seeman and David Bourg

• ### Alignment

Alignment implies that we want all the units in a flock to head in generally the same direction. To satisfy this rule, each unit should steer so as to try to assume a heading equal to the average heading of its neighbors. Referring to Figure 4-6, the bold unit in the center is moving along a given heading indicated by the bold arrow attached to it. The light, dashed vector also attached to it represents the average heading of its neighbors. Therefore, for this example, the bold unit needs to steer toward the right.

We can use each unit's velocity vector to determine its heading. Normalizing each unit's velocity vector yields its heading vector. Example 4-7 shows how the heading data for a unit's neighbors is collected. The line Vave += Units[j].vVelocity; accumulates each neighbor's velocity vector in Vave in a manner similar to how positions were accumulated in Pave.

Example 4-9 shows how the alignment steering force is determined for each unit. The code shown here is almost identical to that shown in Example 4-8 for the cohesion rule. Here, instead of dealing with the average position of neighbors, the average heading of the current unit's neighbors is first calculated by dividing Vave by the number of neighbors, N. The result is stored in u and then normalized, yielding the average heading vector.

Example 4-9. Alignment rule
.
.
.
// Alignment Rule:
if(DoFlock && (N > 0))
{
Vave = Vave / N;
u = Vave;
u.Normalize();
v = Units[i].vVelocity;
v.Normalize();
w = VRotate2D(-Units[i].fOrientation, u);
if(w.x < 0) m = -1;
if(w.x > 0) m = 1;
if(fabs(v*u) < 1)
Fs.x += m * _STEERINGFORCE * acos(v * u) / pi;
}
.
.
.
Next, the heading of the current unit, Units[i], is determined by taking its velocity vector and normalizing it. The result is stored in v. Now, the average heading of the current unit's neighbors is rotated from global coordinates to local coordinates fixed to Units[i] and stored in vector w. The steering direction factor, m, is then calculated in the same manner as before. And, as in the cohesion rule, the alignment steering force is accumulated in Fs.x.

In this case, the steering force is a linear function of the angle between the current unit's heading and the average heading of its neighbors. Here again, we want small steering corrections to be made when the current unit is heading in a direction fairly close to the average of its neighbors, whereas we want large steering corrections to be made if the current unit is heading in a direction way off from its neighbors' average heading.