# Pathfinding avoid a list of polygon

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, i'm working on a 2D RTS project, no tile-based map. I have a problem on building map data structure & algorithm for that. I already think of a naive algorithm to solve that. Of course it's very slow. I'm looking for an advice from who experienced

At very first, i have a list of obstacle as polygon like that

[attachment=20213:map.png]

i decided to build a pathfinding graph, then i travel all pairs of node, connect them if the connection segment doesn't intersect any polygon. this process have O(n^2) time complexity.

[attachment=20214:graph.png]

after that, on every path query, i try connect every node to A(startnode) and B(goal node), then do dijstra to find path. this process have O(2n) + O(nlogn) time complexity.

[attachment=20216:query graph.png]

[attachment=20217:path.png]

That's all, my algorithm works fine(even it's slow) if the environment is static, no insert, remove obstacle. But when the environment turn to dynamic, such as a obstacle removed, i need to rebuild pathfinding graph(O(n^2)), my fps extremely decreased, 120 to 10.

Is there a more professional aproach to solve my problem?

Edited by ngoaho91

##### Share on other sites

Consider using A-star instead of dijkstra, it'll be a lot faster, and requires only a small modification to dijkstra's to make it work.

Secondly, don't rebuild the path every frame. Maybe space the rebuilds over 60 frames (1 second apart). You might even do it in a background thread so the main thread can continue with processing the game, and take over the newly generated graph once its ready.

##### Share on other sites

Dynamically changing environments are tough for algorithms like yours and navmeshes.  Though Navmeshes do have some algorithms out there for recutting the mesh dynamically with decent performance, so it might be worth looking into.

But if you are really changing your environment a lot, you might have to fall back to a tilegrid.

##### Share on other sites

@cormet, actually the algorithm build & rebuild graph cause the slow. dijstra are works fine here but i will try a*.

@aardvajk, i was think of that way before, as i see from here, http://www.ai-blog.net/archives/000152.html the generated graph are poor. so when the agents growns, many paths should duplicate. then agents would collide many times. then evade process occurs frequencely and slow down the game.

@ferrous: please show me those algorithms :D

##### Share on other sites

That's all, my algorithm works fine(even it's slow) if the environment is static, no insert, remove obstacle. But when the environment turn to dynamic, such as a obstacle removed, i need to rebuild pathfinding graph(O(n^2)), my fps extremely decreased, 120 to 10.

For your tests, what is N? How many nodes are in your scene? There's two general approaches I would take:

1) You could reduce the number of number of connections you attempt to make within your scene by not actually checking all pairs.

2) You could reduce the cost of making each connection, by working on the collision algorithm or potentially the cost of constructing the graph itself.

Also, it is really N-squared? You're traveling all pairs. That's N-squared alone. Then, for each one, you have this condition "if the connection segment doesn't intersect any polygon". That does not sound like a constant-time condition. How do you collide a line segment against a polygon? How do you search for which polygons to test?

##### Share on other sites

@pink horror: i tested with 65 nodes, and do the test 100 times per frame, i'm too lazy to draw a map 6500 nodes . and yeah right, i forgot to mention intersect condition.

How do you collide a line segment against a polygon?

first i do simple step, collide the aabb of them. if overlap, i do complex step, check collide that line segment to every single line segment of the polygon.

How do you search for which polygons to test?

i'm using quad tree to divide polygon set. the time to find which polygons to test seem cost logN time(n=number of polygon)

Edited by ngoaho91

##### Share on other sites

i'm using quad tree to divide polygon set. the time to find which polygons to test seem cost logN time(n=number of polygon)

If you already have a quad tree, you already have a kind of waypoint system, much like a grid.

Just regard all quads in the tree, which do not intersect with a polygon and which parent is either the root or an quad which intesects a polygon.

Place a waypoint in the center of each of those quads and at the mid of shared edges (choose the shorter edge if quads of different sizes share an edge).

Connect all waypoints on the edges with the waypoint at the center of the according quads.

If you change the terrain (modify polygons) you just need to regard the affected quad tree sections.

Edited by Ashaman73

##### Share on other sites

Why don't you use the good old grid + A* pathfinding?

It is a pretty simple approach, limit the amount of paths calculated and you should have an OK FPS rate.

If you need better approachs you will have to look for a better graph representation for A* than a grid.

http://theory.stanford.edu/~amitp/GameProgramming/ (A LOT of information on A*)

http://theory.stanford.edu/~amitp/GameProgramming/MapRepresentations.html (Map representation only)

Edited by KnolanCross

##### Share on other sites

Alright so you have three major steps to use navmeshes.

1.  navmesh generation

You can do this manually if you want, or try to autogenerate it yourself, or get a thirdparty library (Recast/Detour)

2.  navmesh traversal (this is basically A* with some smoothing, a lot of the same algorithms apply)

Check Knolan's links above, they do have a Navmesh entry

3.  Navmesh cutting

• 16
• 11
• 23
• 42
• 75