Skip to main content
GameDev.net gamedev.net
🔒 Locked

A* path reconstruction

Started by atcdevil Jul 19, 2005 at 6:52 AM 2 replies 1.3k views
Original Post
atcdevil
atcdevil
I implemented A* based on the wikipedia.org article. It states: "This path reconstruction from the stored closed nodes (see below) means it is not necessary to store the path-so-far within each node." But below that line there is no mention of how to reconstruct the path. So how do I reconstruct the path from the 'closed' priority queue without storing the path so far in each node?
Kris_A
Kris_A
You "traverse in reverse". Whenever you create a node, store its parent node. When you find the node that's at the target position, go into a loop that backtracks the list of nodes (using 'node->parent') until it reaches the first node, adding each node to a list. Then just reverse the list so the path is in the correct order (or if you like you can just go backwards through the list whenever you're making things actually follow the path)

if (node->x == target_x && node->y == target_x) {   while (node != first_node) {      list[list_pos++] = node;      node = node->parent;   }   reverse(list);}

Hope this helps!
atcdevil
atcdevil
that does, thank you very much.

although that solution is sufficient. is it possible to reconstruct the path without even storing the parent?
Kris_A
Kris_A
Not sure, but I would guess not. If you're trying to optimise for size, I think the best you can get away with is storing the parent as a reference to an array element rather than a pointer.

Edit: You could always try different things, like running the pathfinder in reverse, and restrict it to the nodes already created by the first run. Not sure what results that would produce, though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.