PDA

Click to See Complete Forum and Search --> : (VB6 & VB.Net) A* Pathfinding (Fast and Easy)


Jacob Roman
Dec 6th, 2011, 06:53 PM
You won't find code like this anywhere. I have here a very fast and easy to understand A* Pathfinding algorithm (pronounced A Star) that can easily be ported to any game you are making. It finds the fastest route from the starting point which is your predator to the ending point which is your prey. I have ran into other A* programs in VB and found them to be very ugly. Ran into one I found too dependent on values I did not wanna use and found it wasn't the proper A* structure (not to mention it was nearly impossible to port into my game), and another that was easy to understand, but locks up in certain areas on the map and was hideously slow. Sadly I used that slow and easy method on my Bosskillers game which will be corrected. I organized my code to easily break it down and have it commented. For more documentation on A* Pathdfinding, refer to this website:

A* Pathfinding for Beginners (http://www.policyalmanac.org/games/aStarTutorial.htm)

[EDIT] Massive improvements have been made and this is probably the ONLY A* Pathfinding program that actually includes the monster following the path! And you can move your player through the map. It even has collision for the walls. For optimization purposes, the A* Pathfinding only needs to be computed when the monster moves to a new map coordinate.

[EDIT] Another Improvement has been made. I noticed 2 things. One, that the path disappears once in awhile for a split second even though the predator continues to follow the path. Turns out the player needed a role as well in enabling Compute_AStar_Pathfinding. So whenever the player moves to a new map coordinate, it fires AStar. The other problem was that very very rarely it locked up. Having the player play a roll in Compute_Astar_Pathfinding fixed this as well. No more lock ups. Lockups tend to be the biggest problem with AStar samples and can cause your program to hang. I also changed the order it was in the Game_Loop. So it is now this order:


Keyboard Controls
Clear Screen
Collision
AStar Pathfinding
Follow AStar Path
Render Map
Draw AStar Path
Draw Monster
Draw Player
Lock_Framerate 60
DoEvents


Also another improvement I made is that if the monster trys to leave the map screen, itll stop at the edge. However you can leave the map screen with no problem. Itll stop computing AStar when the player is out of the map boundry and the monster continues following the path it has created for itself. AStar samples I tested had Subscript out of range errors for doing so! So my AStar seems to be the best out there so far on the web. I may port this to VB.Net as well and make other improvements such as how the monster interpolates from one tile to the other. Currently although it works, it has issues when the next path for it to follow is at an angle and theres a wall there so it drags along the wall till it goes to that tile.

Jacob Roman
Dec 6th, 2011, 07:00 PM
Heres a VB.Net version which is similar but not my code.

Jacob Roman
Dec 15th, 2011, 05:58 PM
Huge huge update has been made to this program on VB6. The updated one is located above with description of update

BlindSniper
Dec 16th, 2011, 09:58 AM
Does A* Always find the best path or only an optimal path ?

Jacob Roman
Dec 16th, 2011, 11:14 AM
A* finds the fastest and shortest route for the predator to reach its prey. So yeah the best path. There are other algorithms you can use for the Heuristic as well, but Manhattan is the fastest and most basic.


'Case HeuristicType.Manhattan:
' H = Math.Abs(StartX - EndX) + Math.Abs(StartY - EndY);
' break;

'Case HeuristicType.Diagonal:
' H = Math.Max(Math.Abs(StartX - EndX), Math.Abs(StartY - EndY));
' break;

'Case HeuristicType.Euclidean:
' H = Math.Sqrt(Math.Pow(StartX - EndX, 2) + Math.Pow(StartY - EndY, 2));
' break;

'Case HeuristicType.EuclideanSquared:
' H = Math.Pow(StartX - EndX, 2) + Math.Pow(StartY - EndY, 2);
' break;

'Case HeuristicType.TieBreakerManhattan:
' H = Math.Abs(StartX - EndX) + Math.Abs(StartY - EndY) * m_tieBreaker;
' break;

'Case HeuristicType.TieBreakerDiagonal:
' H = Math.Max(Math.Abs(StartX - EndX), Math.Abs(StartY - EndY)) * m_tieBreaker;
' break;

'Case HeuristicType.TieBreakerEuclidean:
' H = Math.Sqrt(Math.Pow(StartX - EndX, 2) + Math.Pow(StartY - EndY, 2)) * m_tieBreaker;
' break;

'Case HeuristicType.TieBreakerEuclideanSquared:
' H = Math.Pow(StartX - EndX, 2) + Math.Pow(StartY - EndY, 2) * m_tieBreaker;
' break;

BlindSniper
Dec 16th, 2011, 03:03 PM
Ok, thanks for clearing that up, I was mixing algorithmic pathfinding up with genetic programming pathfinding. The latter only gets an acceptable path and not always the best.

Jacob Roman
Dec 20th, 2011, 11:14 AM
More updates located above. Fixed some minor issues.