Good work, although it's a little difficult to see what's going on with the 3D view. It would be nice to have a top-down view too
Quote: "It will pass between 2 walls if movement is one of SW,SE,NW,NE"
Avoiding this isn't too difficult really. When you are stepping diagonally, you just need to check the adjacent cells to see if they are clear. You can do this simply by checking the cells at (CurrentX, NewY) and (NewX, CurrentY). If both of them are blocked then you simply treat the cell as blocked during that iteration.
Suggested optimisations:
- You use a straight-line guess for the distance still to travel. Using a 'manhatten' distance is more efficient, and a produces the same results. You simply add the x and y differences together.
- You are doing the same thing that everyone else has done for A* when you maintain and search your open list (except me of course
).
You are using a simple array, and searching from the start to the end to find the lowest cost cell to check next. This is a very costly way to do it.
Firstly, your lowest cost cells are usually at the end of the array, so you are constantly choosing a new lowest-cost cell.
Secondly, you have to search the whole array every time.
Thirdly, you never remove items from the array, so it always gets more expensive to search as you go on.
I suggest that you take a look at understanding and using a heap for your storage. This will give you a massive speed-up when getting the next lowest-cost cell.
Basically if you have 1000 items in your open list, your method will search all 1000 items for the lowest cost cell, and then leave that cell in the open list (meaning that the list always grows).
If you use a heap instead, then the lowest cost item is always the first item in the array, and removing it will only take up to 9 comparisons and up to 9 moves of data. Also, the heap will only ever be as big as it needs to be. If the number of items doubles, then this will add a single extra comparison and move to the removal operation.
You can check out both my heap example code and my A*/flood search routines in the CodeBase to see how it works in practice. There is a url in the description of the heap code that has a great explanation of how heaps work.
Heap :
http://www.thegamecreators.com/?m=codebase_view&i=604edc86c5cdb7db3d4d2681729356b0
Pathfinding :
http://www.thegamecreators.com/?m=codebase_view&i=56984d6fa64dd67ceb1e01c8d835c957
@DavidT,
If you dead-end when there is actually a path to be found, you are not using true A*. If there is a path, A* finds it.
*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins, source and the Interface library for Visual C++ 6, .NET and now for Dev-C++
http://www.matrix1.demon.co.uk