# How can I generate a navigation mesh for a tile grid?

I haven't actually started programming for this one yet, but I wanted to see how I would go about doing this anyway.

Say I have a grid of tiles, all of the same size, some traversable and some not. How would I go about creating a navigation mesh of polygons from this grid?

My idea was to take the non-traversable tiles out and extend lines from there edges to make polygons... that's all I have got so far. Any advice?

• Technically the grid is pretty much equivalent to a navigation mesh. I suspect you're actually asking for a way to optimise the grid and coalesce adjacent squares. Nov 16, 2012 at 8:50
• @Kylotan Yes that is exactly what I meant, just a way to combine adjacent polygons. Nov 17, 2012 at 4:53

## 2 Answers

Here's one of the methods I came up with when doing navmesh for an RTS game. Note that it is homebrew, no third-party tools were used, it took me about 3 weeks to implement and bugfix:

1. Use Marching Squares algorithm to convert obstacle tiles into outlines. Note that map edges is an outline too and need to be included as well.
2. Reduce number of points in outlines using Douglas-Peucker algorithm (purple lines on the bottom picture)
3. Feed all points into Delaunay triangulation (to get most uniform triangles)
4. Add additional points in empty areas and along the map edges (to get more even navmesh)
5. Check along obstacle outlines and flip polygons produced by Delaunay to match outlines. - Often Delaunay could place triangles (grey) mismatching your outlines (red), then you need to detect and flip them. Adjoining them back into a polygon, split it along outline(s) and triangulate it manually
6. Clip obstacles innards - remove polygons that are within obstacles (pink on the picture above)
7. Fill in connectivity data between remaining triangles and vertices as you need - that's your navmesh.

Result:

Meshes are typically implemented as graphs. If you wish to implement path-finding in a map based on a grid do the following:

Create a graph where each traversable square is represented as a vertex. Each pair of adjacent traversable squares represented as vertices, will have an edge between them. And you're done.

• This is not how navmeshes are typically implemented. The purpose of a navmesh (and, I imagine, the reason the asker even asked their question here) is to optimize the graph down to a minimal required number of polygons (usually triangles) that span the most useful spaces to reduce both the number of steps required to find a good path, and the memory footprint required to define the mesh. A raw implementation would both eat up a ton more memory, and would waste valuable AI processing time. Jul 9, 2016 at 21:21
• You are correct. Of course decimation (polygon reduction) is a reasonable and desired optimization. It is just that when you read the op's question, you are getting the sense he just wants to turn a grid into a graph. Jul 12, 2016 at 9:20
• @AturSams Yes this is the bare basic way and thank you for presenting it. But how do we determine which square is traversable? We do this based on our criteria, eg if the angle between the square's normal vector with the world vector is between certain values? Can you give a couple of examples to make sure I understand? Nov 10, 2020 at 0:11