Jh62
Jh62

Reputation: 334

A-Star algorithm (reconstruct path)

I've managed to implement a* and i got the correct path.

The problem is how i can reconstruct the path from start to end, given that every node (from end to start) has a parent link, but not the first one, so my character doesn't know where to go first.

What I'm doing is returning the closed-list and starting from index 0 to x until I reach the end. This usually works well, but I know there's gotta be another way.


Also, what is the best way to check neighboring nodes?

I've made each node to create a rectangle and see if it intersects and that's how I know they're connected. I also use this technique with the player to know when a node has been reached.

Thanks!!

Upvotes: 0

Views: 4465

Answers (1)

amit
amit

Reputation: 178411

You have your target node (You can simply cache it once it is found).

Go up the tree (using the parent field) until you find a node without it, this node is your root. The path you found by going up the links, is the shortest path in reversed order.

I once addressed a similar question, regarding BFS instead of A*

EDIT: A simple way to reverse the stack back to the original is to put the nodes in a stack while you go from target to source, and when you find the source - start popping elements out the stack.

Upvotes: 1

Related Questions