TExcelTAD
Home › Game AI › Path Smoothing

Path Smoothing

A grid search returns a chain of cell centres. It's the cheapest chain on the grid, but an agent that follows it cell by cell looks robotic: it zig-zags across open floor in a staircase, turns in 45° or 90° steps, and hugs walls. Players read that instantly as "dumb AI". Path smoothing turns the chain into a few straight segments that a human would actually walk, without changing which way round obstacles the path goes.

String pulling with line of sight

The simplest smoother that works well is string pulling: imagine the path is a string pinned at the start and goal, and pull it tight.

  1. Start at the first point (the anchor).
  2. Find the farthest later point on the path that the anchor can see in a straight line, i.e. the segment between them crosses no wall.
  3. Keep that point as the next waypoint, make it the anchor, and repeat until you reach the goal.

Everything hinges on the line-of-sight test. On a grid, walk every cell the segment between two cell centres passes through (a supercover line, which includes both cells when the line crosses exactly through a corner) and fail on the first wall. Plain Bresenham lines skip some of the cells a segment touches, so they can let a path clip a wall corner.

csharp · try it yourself
using System;
using System.Collections.Generic;

string[] map =
{
    "####################",
    "#S.................#",
    "#..................#",
    "#.......###........#",
    "#.......###........#",
    "#.......###........#",
    "#................G.#",
    "####################",
};
var start = (x: 1, y: 1);
var goal = (x: 17, y: 6);
bool Blocked(int x, int y) => map[y][x] == '#';

var path = AStar8();
var smooth = StringPull(path);
Console.WriteLine(
quot;grid path: {path.Count} points, length {Length(path):F2}"); Console.WriteLine(
quot;smoothed: {smooth.Count} points, length {Length(smooth):F2}"); Console.WriteLine("waypoints: " + string.Join(" ", smooth.ConvertAll(p =>
quot;({p.x},{p.y})"))); var grid = Array.ConvertAll(map, row => row.ToCharArray()); foreach (var p in path) if (grid[p.y][p.x] == '.') grid[p.y][p.x] = '*'; for (int i = 1; i < smooth.Count - 1; i++) grid[smooth[i].y][smooth[i].x] = (char)('0' + i); foreach (var row in grid) Console.WriteLine(" " + new string(row)); // Supercover line walk between two cell centres (Red Blob Games' "walk grid" version). bool LineOfSight((int x, int y) a, (int x, int y) b) { int nx = Math.Abs(b.x - a.x), ny = Math.Abs(b.y - a.y); int sx = Math.Sign(b.x - a.x), sy = Math.Sign(b.y - a.y); int x = a.x, y = a.y; for (int ix = 0, iy = 0; ix < nx || iy < ny;) { int decision = (1 + 2 * ix) * ny - (1 + 2 * iy) * nx; if (decision == 0) { if (Blocked(x + sx, y) || Blocked(x, y + sy)) return false; // passing exactly through a corner x += sx; y += sy; ix++; iy++; } else if (decision < 0) { x += sx; ix++; } else { y += sy; iy++; } if (Blocked(x, y)) return false; } return true; } List<(int x, int y)> StringPull(List<(int x, int y)> p) { var result = new List<(int x, int y)> { p[0] }; int anchor = 0; while (anchor < p.Count - 1) { int next = p.Count - 1; while (next > anchor + 1 && !LineOfSight(p[anchor], p[next])) next--; // farthest visible point result.Add(p[next]); anchor = next; } return result; } double Length(List<(int x, int y)> p) { double sum = 0; for (int i = 1; i < p.Count; i++) sum += Math.Sqrt(Math.Pow(p[i].x - p[i - 1].x, 2) + Math.Pow(p[i].y - p[i - 1].y, 2)); return sum; } List<(int x, int y)> AStar8() { double D2 = Math.Sqrt(2); double H((int x, int y) c) { int dx = Math.Abs(c.x - goal.x), dy = Math.Abs(c.y - goal.y); return Math.Max(dx, dy) + (D2 - 1) * Math.Min(dx, dy); // octile } var g = new Dictionary<(int x, int y), double> { [start] = 0 }; var from = new Dictionary<(int x, int y), (int x, int y)>(); var open = new PriorityQueue<(int x, int y), (double f, double h, int order)>(); var closed = new HashSet<(int x, int y)>(); int order = 0; open.Enqueue(start, (H(start), H(start), order++)); while (open.TryDequeue(out var cur, out _)) { if (!closed.Add(cur)) continue; if (cur == goal) break; for (int dy = -1; dy <= 1; dy++) for (int dx = -1; dx <= 1; dx++) { var n = (x: cur.x + dx, y: cur.y + dy); if ((dx == 0 && dy == 0) || Blocked(n.x, n.y) || closed.Contains(n)) continue; if (dx != 0 && dy != 0 && (Blocked(cur.x + dx, cur.y) || Blocked(cur.x, cur.y + dy))) continue; double newG = g[cur] + (dx != 0 && dy != 0 ? D2 : 1); if (g.TryGetValue(n, out double old) && old <= newG) continue; g[n] = newG; from[n] = cur; open.Enqueue(n, (newG + H(n), H(n), order++)); } } var path = new List<(int x, int y)> { goal }; while (path[^1] != start) path.Add(from[path[^1]]); path.Reverse(); return path; }
grid path: 17 points, length 18.07
smoothed:  3 points, length 17.60
waypoints: (1,1) (8,6) (17,6)
  ####################
  #S.................#
  #.*................#
  #..*....###........#
  #...*...###........#
  #....*..###........#
  #.....**1********G.#
  ####################

The * cells are the raw 8-connected path and the digit is the one waypoint that survives smoothing. The search could only move in eight directions, so it went diagonally until it was level with the goal and then straight along the bottom: one hard 45° turn, and a long stretch hugging the wall. The smoothed path aims straight for the corner below the pillar and then straight for the goal, at whatever angle that takes. On longer paths across open ground the difference is much bigger: dozens of grid points collapse into two or three waypoints.

Note: String pulling never makes a path longer, because each shortcut replaces a chain of segments with a straight line between the same two points. It doesn't always find the shortest any-angle path either: it can only shortcut between points of the original path, so the result depends on which equal-cost grid path the search returned.

Keep the agent's size in mind

The test above treats the agent as a point moving between cell centres. A real character has a radius, and a segment that just grazes a wall corner will make it scrape along the wall or get stuck. Common fixes:

Fix How
Inflate the walls mark every cell within the agent's radius of a wall as blocked for the search and the line test (navmeshes do this by eroding the walkable area)
Thick line of sight test two or three parallel lines offset by the radius, or sweep a circle
Smooth on the fly each frame, skip ahead to the farthest path point the agent can currently see, so the path also adapts to being pushed off course

Other smoothing techniques

  • Any-angle search: algorithms like Theta* do the line-of-sight check during the search (a node may take its grandparent as its parent if it can see it), which gives shorter paths than smoothing afterwards, at a higher cost per expansion.
  • Funnel algorithm: on a navigation mesh the path is a corridor of polygons, and the funnel algorithm pulls the string through the portals exactly. This is what engine navigation systems do.
  • Curves: after straightening, round the corners with a spline (see Catmull-Rom Splines) or simply let steering behaviors do it: an agent that arrives at each waypoint with a limited turn rate naturally cuts curves. Check that the rounded corners don't cut through walls.

Tip: Draw both paths in your debug view: the raw search path in one colour, the smoothed one in another. When an agent walks into a wall, you'll immediately see whether the search or the smoother is to blame. See Debugging AI.

Test yourself

In string pulling, which point becomes the next waypoint?

  1. The next cell on the path
  2. The farthest later point on the path that is in line of sight of the current anchor
  3. The midpoint of the path
  4. The closest wall corner

Why use a supercover line instead of a plain Bresenham line for the line-of-sight test?

  1. It's faster
  2. It visits every cell the segment touches, including both cells at a corner crossing, so the path can't clip a wall
  3. It draws curves
  4. Bresenham doesn't work on grids

Your smoothed path makes a large character scrape along a wall corner. What's a standard fix?

  1. Use BFS instead of A*
  2. Inflate the obstacles by the agent's radius for the search and the line-of-sight test
  3. Remove the smoothing
  4. Increase the heuristic weight