S M T W T F S
1
2345678
9101112131415
16171819202122
2324252627 28 29
30

Pathfinding Part 2

See Part 1 for background: https://www.gamedev.net/blog/717/entry-2255124-pathfinding-part-1/

OK, so I had reached the point of frustration with my old pathfinding… what to do about it? It had become obvious to me that my pathfinding would have to do some searching to find a path to the target, rather than just walking towards the target semi-blindly.

The classic pathfinding algorithm is called A*. Simplified: A* does a search of the pathfinding space, optimizing the search such that it looks first at paths that go towards the target. It is relatively efficient and quite effective. I won't try to give a full description of A* as that has already been done, many places, very well. Here's one example from the comments: http://www.policyalmanac.org/games/aStarTutorial.htm

However, I had some concerns about A*:
• While efficient as far as search algorithms go, it is still a search algorithm. Running it every "tick" (generally 60 times per second) for every actor in a SENG game would have some cost.
• A* works very well for one entity searching in a static environment. However, as few as 2 entities can get stuck doing A*, constantly running into each other without ever finding the target. This is solveable, but I'd already solved these issues with my heuristic.
• Most importantly: A* works on a discrete space, like a chess-board. SENG has a continuous movement space. The Internet suggests some ways to deal with this, but it isn't a clear cut-and-dried algorithm. Mostly, "first, divide your space into discrete chunks".

A quick note: SENG uses a tile system for basic terrain (2.5 feet x 2.5 feet tiles). However, "objects" (like tables) and actors can have arbitrary position. Movement of actors is in any direction. Disallowing movement except along tiles (in order to implement A*) would be a significant step backwards for the game engine.

I decided to do a combination of A* and my heuristic:
• If we are close to the target, or far from the target, just use the "move towards the target" heuristic.
• Run A*, just using the tiles of the area (ignoring other actors).
• If A* fails (no path to the target, or path too long), just move towards the target.
• Pick the point on the A* path 12.5 feet from the moving actor. Call this the waypoint.
• Use the heuristic movement to move towards the waypoint until we are halfway there, or if we aren't making progress towards the waypoint.
• Go back to (1).

OK, here's an example. In this picture, the actor wants to follow the green path. A* calculates the blue path as the optimal path to the target. (Note that this path would actually be a jagged line due to the tile based nature of the algorithm, but I didn't feel like drawing that). The red path represents the waypoint that the actor has selected to move towards.

In this picture, the actor has gotten halfway to the waypoint, so he recalculates:

And, in this last picture, the actor got halfway to the next waypoint. Note that the waypoint is now starting to move around the corner; however, the heuristic movement is capable of negotiating simple obstacles and will "bounce" around the corner of lava in the way.

I am very pleased; this new pathfinding algorithm combines many of the good features of A* and the heuristic:
• Very efficient. We generally only perform a search of the area tiles every few seconds (per actor). There is no noticeable performance impact of this pathfinding algorithm vs the heuristic.
• Works well with moving targets and moving obstacles. The heuristic algorithm, with its simple minded directed movement, deals quite well with this. A* is restricted to the long-range planning where these problems aren't a factor.
• Movement is continuous, so the actor moves directly towards the target with no zig-zagging. There is actually just a little visible zig-zagging due to waypoints always being the center of a tile, but it is barely noticeable.
• Properly moves around indentations, long walls and other complex formations, due to use of A*.

When I next release a beta of 10 Fantasy Fights, you can experience for yourself!
Sep 28 2012 11:09 PM
Cannot wait until the next beta!!!
Sep 30 2012 03:51 PM
Very interesting to read, we just got done with pathfinding ourselves and it was a bitch!

Note: GameDev.net moderates comments.