Jump to content
  • Advertisement
Sign in to follow this  

A* Pathfinding

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all,


I recently undertook an ambitious goal of implementing A* pathfinding in my aliens AI


While the result in my opinion for a first attempt has been relatively successful, I still have questions that need to be answered. 


Thats why I'm here to talk to the best ;) 


My aliens traverse a level consisting of tiles that carry a hurestic, gCost, and a final FCost given by 


fCost = hurestic + gCost;


My aliens will choose the 8 nodes around their position and choose the one with the lowest fCost.




I am using the manhattan method for the gCost and hurestic.


the hurestic is given by 


diffTileX = StartTileX - TargetTileX

diffTileY = StartTileY - TargetTileY


huresticMulitplier = abs(diffTileX) + abs(diffTileY);


hurestic  = 10 * huresticMultiplier;


the gCost is given by 


a orthongonal move is 10 + the accumulative gCost of parent Node

a diagonal       moveis 14 + the accumulative gCost of parent Node




My alien will pick the node in the list (after a bubble sort) that has the lowest f-Cost, no more, no less.




My alien DOES find my position, but does not take a direct route AT ALL!


Should there be futher testing other then just an f-Cost?


If so, then what? Should I check if the path that leads to the target has a incrementally dropping hurestic?


I will include a picture to show the aliens path during run-time



Share this post

Link to post
Share on other sites

This may not have been the correct place to post this topic, ill move it if asked.


Also as you can see the Alien will always find the target, but will take a very unconcise path

Share this post

Link to post
Share on other sites

I figured it out....sorta.


Once all of the nodes have been recorded with the lowest f-Cost, then I trace the path from the target and check the adajecent nodes while tracing and only trace the one with the lowest g-Cost to eliminate redundant moves. However, I feel this is the most brute-force way to approach it. Does anybody know a better one?


I mean, I am check against all the nodes in the path to see if there adajecent to that current square (this involves several for-loops that iterate several hundred times). once I get all the squares adajcent to the current square, I then check to see which has the lowest g-Cost. If only one node is adajecent to the current node, then I dont bother doing any testing and add it to the final path.


Is there a better way to approach this? Like when I am recording the f-Cost maybe instead of offsetting until after the fact?






    For what it's worth the alien does take a now much better path :D




Share this post

Link to post
Share on other sites

In order to find the optimal route, you need to implement A* somewhat differently.


First off, your heuristic needs to underestimate the cost after a candidate step - at the moment, I suspect that it doesn't.


Secondly, to find the optimal path, the A* algorithm needs to explore completely to the target. You need to keep a heap of <fCost,path> tuples, and iterate popping the least cost path off the heap, enumerating all the valid moves from that point, and pushing updated tuples onto the heap until you reach the goal.


You can't make a single iteration of the above loop and then make a movement choice based upon that.


A* is essentially a breadth-first search of the tree of moves, with the heuristic serving to delay searching likely unprofitable paths.


What it sounds like you're implementing currently (by fixing a move choice at each step) is more like depth-first search.

Share this post

Link to post
Share on other sites

The algorithm that I am currently using is as you guessed does indeed overestimate it.


I like your suggestion of using the tuples. I feel this is once again a instance where I jumped head first into a concept way over my head without thinking about it enough.


My implementation of A* works now and DOES find a concise path, but at the cost of I fear performance, and the creeping suspicion of knowing that Im going about it as you mentioned the least optimal way.

Ill switch my hurestic to underestimate . And eventually revise the entire algorithm to embody a breadth-first search as opposed a depth-first search (which I am going to google after this reply ph34r.png


But your completely right, my hurestic overestimates it, and I choose a step every iteration.


Thanks a bunch for the reply,



Share this post

Link to post
Share on other sites

The good thing is that you don't need to recompute paths all that often. You should be able to implement it efficiently enough for realtime use.


Definitely you should use a heap rather than bubble sort. Bubble sort is only ever a good idea for almost sorted data. Its average time complexity is O(n^2), which means that it will quickly dominate the pathfinding. A heap, in contrast, can be used to completely sort in O(nlog(n)) time, and incremental operations like adding and removing values, it's O(log(n)).


Another useful optimization is to break your map up into convex regions, because the shortest path in a convex region is straightforward. Thus you can take bigger steps (crossing an entire convex space in one move) and potentially reach your goal with less exploration.

Share this post

Link to post
Share on other sites

Alright, I think I got it.


The alien will always find the shortest path (IF THERE IS ONE (Denoted by no more nodes in the OpenList)) 


For the sake of visual representation, I drew on the tiles to show nodes contained in the OpenList, ClosedList, and the FinalPath traced back from the target to 

each parent back to the start node


In the last photo you'll see the alien went off the map to find the shortest path, that of course will be reprimanded.


I'm glad I got it working correctly, now I can work on aliens decision making and other areas of the AI




Red is the openList, 

Blue is the ClosedList

yellow is the finalPath

Edited by markypooch

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!