Flow Fields
An RTS player selects 200 units and clicks one spot on the map. Running A* 200 times, from 200 starts to the same goal, mostly repeats the same work. A flow field turns the problem around: run one search from the goal over the whole map, and store at every cell the direction that leads downhill towards it. Then every unit, wherever it stands, just reads the arrow under its feet. Supreme Commander 2 (2010) popularized the technique for RTS games, and it's the standard answer for crowds and swarms heading to shared destinations.
Three layers
| Layer | What each cell stores | Built by |
|---|---|---|
| Cost field | the cost of entering the cell (1 floor, 3 mud, impassable for walls) | the level, or designers painting terrain |
| Integration field | the total cost of the cheapest path from the cell to the goal | Dijkstra started at the goal, with no early exit |
| Flow (direction) field | which neighbour to step to: the one with the lowest integration value | one pass over the integration field |
Because the integration field is a Dijkstra flood from the goal, every cell's value is its exact distance-to-goal. The direction field just points each cell at its cheapest neighbour, so following the arrows from anywhere traces a cheapest path.
Building a flow field
The cost field has floor (., cost 1) and mud (~, cost 3). The integration field is built with 4-way moves; the arrows may point to any of the 8 neighbours (no corner cutting), which gives smoother motion:
using System;
using System.Collections.Generic;
string[] map =
{
"############",
"#..........#",
"#..###.....#",
"#....#~~#..#",
"#....#~~#G.#",
"#..........#",
"############",
};
int w = map[0].Length, h = map.Length;
var goal = (x: 9, y: 4);
bool Wall(int x, int y) => map[y][x] == '#';
int Cost(int x, int y) => map[y][x] == '~' ? 3 : 1;
// Integration field: Dijkstra from the goal over the whole map.
var dist = new int[h, w];
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) dist[y, x] = int.MaxValue;
dist[goal.y, goal.x] = 0;
var pq = new PriorityQueue<(int x, int y), (int d, int order)>();
int order = 0;
pq.Enqueue(goal, (0, order++));
var four = new (int dx, int dy)[] { (0, -1), (1, 0), (0, 1), (-1, 0) };
while (pq.TryDequeue(out var c, out var pri))
{
if (pri.d > dist[c.y, c.x]) continue;
foreach (var (dx, dy) in four)
{
int nx = c.x + dx, ny = c.y + dy;
if (Wall(nx, ny)) continue;
int nd = pri.d + Cost(c.x, c.y); // cost of the cell being entered when walking towards the goal
if (nd < dist[ny, nx]) { dist[ny, nx] = nd; pq.Enqueue((nx, ny), (nd, order++)); }
}
}
Console.WriteLine("integration field (cost to the goal):");
for (int y = 0; y < h; y++)
{
var line = "";
for (int x = 0; x < w; x++) line += Wall(x, y) ? " #" : quot;{dist[y, x],3}";
Console.WriteLine(line);
}
// Flow field: each cell points at its cheapest neighbour (8 directions, no corner cutting).
var eight = new (int dx, int dy, char arrow)[]
{
(0, -1, '↑'), (1, -1, '↗'), (1, 0, '→'), (1, 1, '↘'), (0, 1, '↓'), (-1, 1, '↙'), (-1, 0, '←'), (-1, -1, '↖'),
};
var flow = new (int dx, int dy)[h, w];
Console.WriteLine("flow field:");
for (int y = 0; y < h; y++)
{
var line = "";
for (int x = 0; x < w; x++)
{
if (Wall(x, y)) { line += '#'; continue; }
if ((x, y) == goal) { line += 'G'; continue; }
int best = dist[y, x];
char arrow = '?';
foreach (var (dx, dy, a) in eight)
{
if (Wall(x + dx, y + dy)) continue;
if (dx != 0 && dy != 0 && (Wall(x + dx, y) || Wall(x, y + dy))) continue;
if (dist[y + dy, x + dx] < best) { best = dist[y + dy, x + dx]; arrow = a; flow[y, x] = (dx, dy); }
}
line += arrow;
}
Console.WriteLine(" " + line);
}
// Every unit just follows the arrow under it.
var units = new List<(int x, int y)> { (1, 1), (1, 5), (4, 3), (10, 1) };
for (int tick = 0; tick <= 8; tick++)
{
Console.WriteLine(quot;tick {tick}: " + string.Join(" ", units.ConvertAll(u => quot;({u.x},{u.y})")));
for (int i = 0; i < units.Count; i++)
if (units[i] != goal) units[i] = (units[i].x + flow[units[i].y, units[i].x].dx, units[i].y + flow[units[i].y, units[i].x].dy);
}integration field (cost to the goal):
# # # # # # # # # # # #
# 11 10 9 8 7 6 5 4 3 4 #
# 12 11 # # # 5 4 3 2 3 #
# 11 10 9 8 # 6 5 # 1 2 #
# 10 9 8 7 # 5 4 # 0 1 #
# 9 8 7 6 5 4 3 2 1 2 #
# # # # # # # # # # # #
flow field:
############
#→→→→→↘↘↘↓↙#
#↗↑###→→→↓↙#
#↘↘↘↓#↗↑#↓↙#
#↘↘↘↓#↘↓#G←#
#→→→→→→→→↑↖#
############
tick 0: (1,1) (1,5) (4,3) (10,1)
tick 1: (2,1) (2,5) (4,4) (9,2)
tick 2: (3,1) (3,5) (4,5) (9,3)
tick 3: (4,1) (4,5) (5,5) (9,4)
tick 4: (5,1) (5,5) (6,5) (9,4)
tick 5: (6,1) (6,5) (7,5) (9,4)
tick 6: (7,2) (7,5) (8,5) (9,4)
tick 7: (8,2) (8,5) (9,5) (9,4)
tick 8: (9,2) (9,5) (9,4) (9,4)
Look at the mud: the pocket in the middle is the shortest way down for a unit at the top of the map, but stepping into it costs 3, so the arrows above it point right, round the wall, instead. The four units started in different corners and needed no searches of their own: four table lookups per tick, whether there are four units or four thousand.
Note: The flood runs backwards, from the goal outwards. When it steps from cell
cto a neighbourn, it adds the cost ofc, because a unit walking forwards fromnwould be enteringc. Getting this backwards is a classic bug: units then avoid standing in mud instead of avoiding walking into it. Pick one convention and use it in the cost field, the search and your debug drawing.
Step through the flood
The integration field grows outward from G (each label is the cost to reach the goal; mud ~ costs 3 to enter), and the last step turns it into arrows. This animation uses 4-way arrows to keep it small:
Making it production-ready
| Problem | Usual solution |
|---|---|
| Units moving cell to cell look jerky | treat the arrow as a desired velocity for steering, or interpolate the flow vectors between cell centres |
| Units pile into each other | add separation steering on top of the flow (see Obstacle Avoidance & Separation) |
| The map is huge | split it into sectors, plan a coarse path over sector portals first, and build flow fields only for the sectors on that route; cache them by (sector, exit) |
| Many different goals | flow fields pay off when many units share a goal; for a few units with their own goals, A* per unit is cheaper |
| A wall appears (a door closes, a building is placed) | rebuild only the affected sectors, ideally spread over several frames |
A useful extension is the line-of-sight pass: cells that can see the goal in a straight line store "go straight to the goal" instead of an 8-way arrow, which removes the last bit of grid-shaped motion near the destination.
Flow fields and Dijkstra maps
Roguelike developers know the integration field as a Dijkstra map: every monster rolls downhill on it to reach the player. The same field run with negated, scaled values makes monsters flee intelligently towards exits rather than into corners, and adding several fields together (one for the player, one for treasure, one for danger) gives cheap emergent behaviour. It's the grid cousin of an influence map.
Test yourself
Where does the search that builds a flow field start?
- At each unit
- At the goal, flooding outwards over the whole map
- At the map's corner
- At the nearest wall
How does a cell's arrow get chosen?
- Towards the goal in a straight line
- Towards the neighbour with the lowest integration (cost-to-goal) value
- Towards the nearest unit
- Alternating directions
When is a flow field a worse choice than per-unit A*?
- When 500 units share one destination
- When a few units each have a different destination on a large map
- On grid maps
- When terrain has costs