0

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!!

Jh62
  • 324
  • 1
  • 3
  • 15
  • 1
    *"given that every node (from end to start) has a parent link, but not the first one"* - so, uh, give the first one a parent link then... – BlueRaja - Danny Pflughoeft Sep 13 '12 at 07:52
  • If you give the first one a parent link to the second one, the second one parent is still the first one, so it will generate an endless loop... I know what I can do, I was asking if there's a simple way (known) way to do it. I think I'll stick with the index i mentioned before. – Jh62 Sep 14 '12 at 01:59
  • I think you are not keeping track of parents correctly... the last node will have a `PreviousNode`, which will have a `PreviousNode`, which will have a `PreviousNode`... until the start, which has no `PreviousNode` (it's null). That's your start square. There's no endless loop, and there's no part of the path missing. What are you doing differently? – BlueRaja - Danny Pflughoeft Sep 14 '12 at 05:47

1 Answers1

1

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.

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • Yeah, i know that. The problem is how i reconstruct the path when i get to the first node (the source) if it has no link to the second one (given that the parent link only works backwards). – Jh62 Sep 13 '12 at 08:35
  • 2
    @user1656222: You simply go from the *target* to the source backward and gets the reverse path. A simple way to reverse it back is to put all elements as you go in a stack, and when you are done - start popping the nodes from the stack. – amit Sep 13 '12 at 08:37
  • Isn't the same if do it like i explained earlier? Return the closedList and just start from index 0 until reaching the end of the list? – Jh62 Sep 13 '12 at 08:55
  • Any thoughts about neighboring nodes? The rectangle thing works, but its a bit messy, but is useful when you have a map with scattered nodes around. If I divide the whole map into nodes, I can easily connect x node to it's neigbours by just looking at it's ID and doing id+-1 and id+-x depending on the wide of the map. – Jh62 Sep 14 '12 at 02:02
  • @user1656222: It really depends on the exact problem you have on hand – amit Sep 14 '12 at 05:17