A soldier's navmesh path zig-zags between polygon edge midpoints across an open floor, then scrapes along wall corners; what causes each, and how do you fix them?
answer
- a corridor of polygons, not a path
- shared sides act as portals
- pull the string taut
- bends sit on mesh vertices
- radius baked into the mesh
basics
~20 sZig-zag means waypoints sit at polygon centres or edge midpoints; string-pull the polygon corridor with the funnel algorithm. Scraping means the taut path touches vertices of a mesh not eroded by the agent's radius; erode at build time.
solid answer
~50 sA* on a navmesh returns a **corridor**, a sequence of polygons, not a path; walking it through centres or edge midpoints produces the zig-zag. The **funnel algorithm** turns the corridor into the shortest path inside it: the shared sides between consecutive polygons are portals; from an apex at the start it keeps a left and a right boundary, narrows them portal by portal, and when a new endpoint would cross the opposite side, that opposite side's point becomes a corner of the path and the new apex. The result is a taut string whose bends sit exactly on portal endpoints, mesh vertices usually at wall corners. If the mesh was eroded by the agent radius, those vertices are already a radius from the wall and the agent's centre can pass through them; if not, the body scrapes, so fix the build, or as a stop-gap shrink each portal by the radius before pulling.
code
pseudocode · 45 lines// 2D, viewed from above: x right, y forward.
// cross(o, a, b) > 0 when b lies to the left of the ray from o through a.
function cross(o, a, b):
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
// portals[i].left / .right as seen walking from start to goal;
// portals[0] = (start, start), portals[last] = (goal, goal)
function funnel(portals):
apex = portals[0].left
left = apex; right = apex
apexIndex = 0; leftIndex = 0; rightIndex = 0
path = [apex]
i = 1
while i < length(portals):
L = portals[i].left
R = portals[i].right
// try to move the right side inward
if cross(apex, right, R) >= 0:
if apex == right or cross(apex, left, R) < 0:
right = R; rightIndex = i
else:
// R crossed the left side: the left point is a corner
path.append(left)
apex = left; apexIndex = leftIndex
left = apex; right = apex
leftIndex = apexIndex; rightIndex = apexIndex
i = apexIndex + 1
continue
// try to move the left side inward
if cross(apex, left, L) <= 0:
if apex == left or cross(apex, right, L) > 0:
left = L; leftIndex = i
else:
// L crossed the right side: the right point is a corner
path.append(right)
apex = right; apexIndex = rightIndex
left = apex; right = apex
leftIndex = apexIndex; rightIndex = apexIndex
i = apexIndex + 1
continue
i = i + 1
goal = portals[length(portals) - 1].left
if path.last != goal:
path.append(goal)
return pathgo deeper
Recall that A* on a navmesh gives a list of polygons, and that a separate straightening step turns it into a path.
Walk through the funnel: portals, apex, left and right sides, and the moment a crossing side becomes a corner.
Diagnose both symptoms from the path's shape: midpoint weaving means no string pulling, corner scraping means radius was never baked into the mesh, and name the build fix over a runtime patch.
Set the standard for how agent size is handled across the game, size classes and erosion at build time, so path quality does not depend on per-agent patches.
## From corridor to path Searching a navmesh means searching its polygon graph: nodes are convex polygons and edges join polygons that share a side. A* returns a **corridor**, the ordered list of polygons from the start polygon to the goal polygon. That is not yet a path. The simplest way to make one is to steer through each polygon's centre or each shared side's midpoint, and that is exactly what produces the zig-zag: midpoints of successive sides alternate left and right of the straight line, so the soldier weaves across open floor. The shared side between two consecutive polygons is a **portal**: the path must cross it somewhere, but anywhere along it is allowed. The shortest path through the corridor is the one a string would take if pulled taut from start to goal through every portal. ## The funnel algorithm The **funnel algorithm**, also called string pulling, computes that taut path in a single sweep over the portals that backs up only when it adds a corner; in practice its cost is close to linear in the corridor's length, far below the search that produced the corridor. 1. Start with the **apex** at the start point, and the funnel's left and right sides both at the apex. 2. For each next portal, try to move the right side to the portal's right endpoint. Accept it if it narrows the funnel and stays right of the left side. 3. If the new right endpoint crosses over the left side, the path cannot go straight past the left point: append the left point as a **corner**, make it the new apex, reset both sides to it, and resume from the portal after it. 4. Do the same for the left side, with the roles swapped. 5. After the last portal, which is the goal itself, append the goal. The "left of" and "right of" tests are signs of a 2D cross product, a geometric primitive taught with game mathematics; the algorithm only needs the sign. ## What the result is, and is not - It is the **shortest path inside the corridor**. Every bend sits on a portal endpoint, so on a mesh vertex. - It is **not necessarily the shortest path on the whole mesh**. A* chose the corridor using approximate edge costs, such as distances between polygon centres, so a different corridor can hold a shorter true path. Better edge-cost estimates narrow the gap. - It does not avoid other agents; moving people around each other is the job of local avoidance, which works on top of the path. - It needs the corridor's portals with consistent left and right ends as seen from the start; a corridor whose portals are flipped produces paths that cut through walls, so build the portal list carefully from the polygon adjacency. ## Why the soldier scrapes corners Because bends sit on mesh vertices, the soldier's **centre** passes exactly through the corners of the walkable area. Whether that is safe depends on how the mesh was built. | Mesh | Where the vertex is | Result | |---|---|---| | Eroded by the agent radius | One radius from the wall corner | Body just clears the wall | | Not eroded | On the wall corner itself | Body overlaps the wall and scrapes | | Eroded for a smaller agent | Less than this agent's radius away | Large agents clip, small ones are fine | Fixes, in order of preference: - **Erode the mesh** by the agent radius at build time, with a mesh per size class. - If that is not possible, **shrink each portal** by the radius from both ends before pulling, so corners land roughly a radius inward. This is approximate: at sharp corners the body can still come closer than one radius to the wall. A portal narrower than twice the radius collapses or inverts, which is the signal that the agent does not fit. - Add a small extra margin only if steering tends to overshoot corners; do not hide a missing erosion with path-following tricks. ## Other sources of ugly paths - **Grid paths** zig-zag for a different reason: moves are limited to 45 or 90 degrees. A line-of-sight post-pass that removes waypoints whose neighbours can see each other, or an any-angle search such as Theta*, straightens them. - **Splines through midpoints** look smooth but can leave the walkable area at corners; smooth after string pulling, and check the result stays on the mesh. - **Following the path badly**: an agent should steer toward the next corner and advance when it reaches it or can see past it, not stop at each one.
- How do you smooth a path on a grid rather than a navmesh?Grid paths are limited to 45- and 90-degree steps. A cheap post-pass walks the waypoints and drops any whose neighbours see each other through walkable cells, giving fewer, longer segments. An any-angle search such as Theta* folds that line-of-sight check into the search, letting a node take a parent it can see directly.
- Why can the funnel output still be longer than the true shortest path on the mesh?The funnel is optimal only within the corridor it is given. A* picked that corridor using approximate polygon-to-polygon costs, such as centre distances, so a neighbouring corridor may contain a shorter taut path. Costing edges by better estimates, or a local refinement around the chosen corridor, reduces the gap.
- What happens when a portal is narrower than the agent on an un-eroded mesh?Shrinking that portal by the radius from both ends inverts it, so the corridor does not fit the agent at all. The funnel will still produce a path through the gap, which is why radius must be handled at build time or the search must reject portals narrower than the agent's width.
Pulling a string taut through a row of doorways: wherever you hold the ends, the string bends only on the door frames. If the frames are the real walls, a person walking that line brushes every frame; if they are set back by half the person's width, the person just clears them.
- Walking through polygon centres gives the shortest path through the corridor.
- The funnel output is the shortest path on the whole navmesh.
- Corner-hugging is a bug in the funnel algorithm itself.
- Adding more, smaller navmesh polygons fixes the zig-zag.
- A spline through the edge midpoints is a safe way to smooth the path.