• Create Account

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Sparkon  Members   -  Reputation: 395

Like
0Likes
Like

Posted 22 October 2012 - 06:57 AM

Hello guys... i'm now working on the pathfinding of my game, and since pathfinding can really slow down the game i had three ideas :
1) i update the game (rendering) at 60 fps ( example ), i update my physics at 60/2 and since pathfinding does not require to be updated many times a second i was gonna update it once every 60 frames
3) splitting pathfinding algorithms over multiple frames

I've read that multithreading is used often when dealing with pathfinding or similar, but i've got only some basic knowledge about multi-threading (C++)
The 1) IMHO is a good idea... but it can still slow down my program in THAT frame i update the pathfinding.
The 3) seems the best idea for me... splitting a N number of paths to find over N threads, but i have really no good idea how to implement it.

i was looking for some hints & tips from people with more experience. My game is pretty simple, but the AI is the most important part (it's for learning mainly). And for pathfinding i'm using different algorithms for different purposes ( DFS,BFS,Dijkstra,A*). i've already implemented most of them and even the DFS that "is supposed to be the faster" really slow the program down.

P.S. if you have any good article i'd be glad to read it !

Edited by Sparkon, 22 October 2012 - 06:59 AM.

### #2Álvaro  Crossbones+   -  Reputation: 13897

Like
1Likes
Like

Posted 22 October 2012 - 08:03 AM

I would spend some time on this idea:
0) Make your pathfinding code as fast as possible.

I don't know anything about how you code, but I have seen enough horror stories to suspect that there is a decent chance you are doing something needlessly slow in your code. I am thinking of something like allocating dynamic memory whenever a node gets visited, or that type of thing.

I would only pursue one of the other options after I have satisfied myself that there isn't much to be gained from improving the single-threaded pathfinding code.

### #3Olof Hedman  Crossbones+   -  Reputation: 2948

Like
1Likes
Like

Posted 22 October 2012 - 08:07 AM

Before you start to multithread, make sure your algoritms are working properly and efficiently with extensive tests.

Then reduce the times it is run, why would it be needed to run at a fixed timestep?
Only times the path changes is if the goal changes or the terrain changes.

Then you can simplify the terrain to have less nodes to search.

DFS is not "supposed to be faster", only in very special cases.

You probably want A*, specially if you have an easy way to estimate distance left to goal, dijkstra's is useful if you can't.

Edited by Olof Hedman, 22 October 2012 - 08:22 AM.

### #4Sparkon  Members   -  Reputation: 395

Like
0Likes
Like

Posted 22 October 2012 - 09:54 AM

Ahah Alvaro hopefully i havn't done it yet.
Anyway i'm already trying to optmize that. To get all the nodes in the map i've used flood fill... I'll try to reduce the number of nodes. I've got one node every 15 pixels ( except for walls or non-walkable areas ) that roughly are 8k nodes per map. I'm realizing that maybe they are too many nodes.
The entire map is around 1920x1080. Do you think that are too many ?

### #5Olof Hedman  Crossbones+   -  Reputation: 2948

Like
0Likes
Like

Posted 22 October 2012 - 10:29 AM

That doesn't sound much to me... thats just a grid of approx 90x90 nodes.

If the map is relatively open and interconnected, an A* shouldn't have to visit much more then a few hundred in average, and that is when navigating from one end of the map to the other. If start and end is relatively close to eachother, you will just visit a handful of nodes, should be very fast.

DFS will in average visit half the nodes, even when start and end is just next to each other (if you are unlucky and start searching the wrong direction, you might have to visit _every_ node before you find the goal in your neighbor node...)

I think there might be something funky in your A*
unless you have a very high number of agents pathfinding...

Another optimization is to have different levels of pathfindning, a coarse one to find (for example) the "rooms" to visit, and then a finer to navigate through each "room".

Edited by Olof Hedman, 22 October 2012 - 10:42 AM.

### #6Sparkon  Members   -  Reputation: 395

Like
0Likes
Like

Posted 22 October 2012 - 10:52 AM

That doesn't sound much to me... thats just a grid of approx 90x90 nodes.

If the map is relatively open and interconnected, an A* shouldn't have to visit much more then a few hundred in average, and that is when navigating from one end of the map to the other. If start and end is relatively close to eachother, you will just visit a handful of nodes, should be very fast.

DFS will in average visit half the nodes, even when start and end is just next to each other (if you are unlucky and start searching the wrong direction, you might have to visit _every_ node before you find the goal in your neighbor node...)

I think there might be something funky in your A*
unless you have a very high number of agents pathfinding...

Another optimization is to have different levels of pathfindning, a coarse one to find (for example) the "rooms" to visit, and then a finer to navigate through each "room".

A* definitively faster than DFS ( i'm finishing A* "benchmarks" ). DFS takes 1 second in the worst case

### #7Olof Hedman  Crossbones+   -  Reputation: 2948

Like
0Likes
Like

Posted 22 October 2012 - 11:28 AM

Sounds to me like that inner loop can be optimized, if you only test 8 nodes per millisecond. (assuming modern PC)
Remember, adding threads will only increase capacity by the number of cores you have.
Optimizing your inner loop could give you 100x improvement in some cases (not saying you could in this case)

What is your heuristic for A*? if its euclidian distance to goal, you could try squared distance, just as good but a lot faster.

There are also more advanced ones to handle dead ends better if your map have lots of those, you could check this out for example: http://www.ru.is/fac...jornssonH06.pdf

Though, when you've exhausted all algorithmic improvements and optimizations, at least it IS easily parallelized.
For multithreading, you should remember that you can't run more threads then cores in parellel, so you should divide n paths per thread where n = number of paths / number of cores.
some kind of thread pool that you dispatch work to, seems like a popular architecture for those things, but I must admit I don't have much experience in that.

Edited by Olof Hedman, 22 October 2012 - 11:36 AM.

### #8Sparkon  Members   -  Reputation: 395

Like
0Likes
Like

Posted 22 October 2012 - 11:36 AM

Sounds to me like that inner loop can be optimized, if you only test 8 nodes per millisecond. (assuming modern PC)
Remember, adding threads will only increase capacity by the number of cores you have.
Optimizing your inner loop could give you 100x improvement in some cases (not saying you could in this case)

What is your heuristic for A*? if its euclidian distance to goal, you could try squared distance, just as good but a lot faster.

There are also more advanced ones to handle dead ends better if your map have lots of those, you could check this out for example: http://www.ru.is/fac...jornssonH06.pdf

Though, when you've exhausted all algorithmic improvements and optimizations, at least it IS easily parallelized, and your assumption of 1 path per thread sounds very good.

For the DFS ( that is causing me most of the trouble ) to get from node 10 to 600 ( it's doing the worst path possible ) with ~15k recursion in the inner loop and ~8400 nodes and ~32600 edges ( only 4 directions ) it takes 1 seconds. Here is my code for the DFS ( for this algorithm i'm following a book )
void GraphSearch::DFSearch(int sourceID,int targetID,std::vector<int>& path)
{
// creating containers
std::stack<const MapEdge*> stack;
std::vector<int> visitedNodes(pNodes_->size(),node::NODE_UNVISITED); // creating the container here is quite slow, but DFS still takes 1 sec
std::vector<MapEdge*> connectingNodes;
sf::Clock clock;
float timeElapsed = 0;
int deep = 0;
MapEdge dummyStart(sourceID,sourceID,10);
stack.push(&dummyStart);
while(!stack.empty())
{
timeElapsed += clock.restart().asSeconds();
deep += 1;
const MapEdge* next = stack.top();
stack.pop();
if (next->To() == targetID)
{
std::cout << "Time elapsed " << timeElapsed
<< " on : " << deep << " recursions" << std::endl;
return;
}
path[next->To()] = next->From();
visitedNodes[next->To()] = node::NODE_VISITED;
connectingNodes.clear();
FindConnectingNodes(next->To(),connectingNodes); // this might be the problem since i'm looping through 32k edges * 15k recursions
std::size_t s = connectingNodes.size();
for(int i = 0;i <s;++i)
{
if (visitedNodes[connectingNodes[i]->To()] == node::NODE_UNVISITED)
stack.push(connectingNodes[i]);
}
}
}


edit : yes the FindConnectingNodes is definitively a problem, i've just given a run with VerySleepy and i've found out that is the most expensive call. something like 20% of the whole program ( windows call included)

Edited by Sparkon, 22 October 2012 - 11:41 AM.

### #9GWDev  Members   -  Reputation: 281

Like
0Likes
Like

Posted 22 October 2012 - 12:20 PM

Hi Sparkon,

are you sure, that you need to do all the calculation every time?

I don't know which game you are building exactly, but there might be a way to use precalculated A* for example.

e. g. if you have many rooms and the player moves in the building as does the enemy (AI). You could calculate the routes between the room at buildtime. Than you just lookup position (enemy room) and destination (player room) and have the best way. You can even have seperate calculations for different conditions like locked doors, spawned health packs etc.

As soon as player and enemy are in the same room you can use nearly any pathfinding algorithm for the 'last mile'.

-- GWDev

Edited by GWDev, 22 October 2012 - 12:26 PM.

### #10Sparkon  Members   -  Reputation: 395

Like
0Likes
Like

Posted 22 October 2012 - 12:53 PM

Hi Sparkon,

are you sure, that you need to do all the calculation every time?

I don't know which game you are building exactly, but there might be a way to use precalculated A* for example.

See http://www.gamedev.n...collection-r723

e. g. if you have many rooms and the player moves in the building as does the enemy (AI). You could calculate the routes between the room at buildtime. Than you just lookup position (enemy room) and destination (player room) and have the best way. You can even have seperate calculations for different conditions like locked doors, spawned health packs etc.

As soon as player and enemy are in the same room you can use nearly any pathfinding algorithm for the 'last mile'.

-- GWDev

yes... i'll probably do as you suggest.

Olof Hedman i fixed the code now it's extremely faster, i'll try to improve it more before going with multi-threading

### #11Olof Hedman  Crossbones+   -  Reputation: 2948

Like
0Likes
Like

Posted 22 October 2012 - 01:03 PM

Yeah, your datastructure is definitely a problem.

A common, and very fast way, let the nodes be cells in an array, like pixels in a bitmap, and just mark cells as "unpassable" for walls and such.
Then finding neighbours is as easy as adding or substracting to the index of the current node.

Edit: didn't see the last post Glad to see you've improved it.

Edited by Olof Hedman, 22 October 2012 - 01:04 PM.

### #12wodinoneeye  Members   -  Reputation: 876

Like
0Likes
Like

Posted 23 October 2012 - 12:09 AM

Much depends on what is dynamic (which effects continuing validity of a path that is found) .

If nothing impacts a path once it is built then the calculated paths dont need to be redone over and over

If the things that impact the path validity move less than once a second (or alot less), then re-calculating the paths much faster than that is a waste.
(You may even be able to detect IF something on the map changed each 'turn' and if it hasnt, then the previously calculated paths can be reused0

When the target is far away. you mau be able to continue to reuse the old calculated path (or only recaclulate it infrequently) until you get close enuf or get stuck on something dynamic. (You may have to find any factors/cases where it DOES matter to recalculate faster).

Usually when you are very close (terminal guidance) to the target is when you need to recalculate very frequently (which usually has to be handled specially anyway)

If you have alot of dynamic elements (things that block paths change alot) then there also can be processing variations that can reuse/patch a saved path data (so as to not require full recalculation).

----

Multi-threading can be useful when you have cores to spare- (and see Processor affinity to help that along) especially when you have many objects that need pathfinding done for them (but with the usual issues of handling any map dynamic changes ).

For huge maps (and long paths) - hierarchical pathfinding can be very important

Edited by wodinoneeye, 23 October 2012 - 12:28 AM.

--------------------------------------------Ratings are Opinion, not Fact

### #13wodinoneeye  Members   -  Reputation: 876

Like
0Likes
Like

Posted 23 October 2012 - 12:27 AM

Yeah, your datastructure is definitely a problem.

A common, and very fast way, let the nodes be cells in an array, like pixels in a bitmap, and just mark cells as "unpassable" for walls and such.
Then finding neighbours is as easy as adding or substracting to the index of the current node.

Edit: didn't see the last post Glad to see you've improved it.

yeah the flagged 'grid' border eliminates the costly edge coordinate tests

I helped someone long ago with every optimzation possible and even went so far as ditching the 2D array, using a single dimensional array/memory block and pointer math +/- 1 for the X axiz and +/- a precaclulated map width for the Y (I forget if we did byte addressing directly to get rid of the pointer multiply altogether- though I do recall an 8 grid direction array with all the single value offsets precalculated) .

The pointers also helped with the open/closed list (one offset value to point to the map data) . Along with other things like using a HeapQ and smallest datas possible to minimize cache misses (which was far more significant than word alignment issues).

It was amazing how much faster we got it to run (and we were using large maps 1K x 1K where you needed the performance)

Edited by wodinoneeye, 23 October 2012 - 12:39 AM.

--------------------------------------------Ratings are Opinion, not Fact

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS