This week I learned Theta* pathfinding algorithms for all-angle movement through square grids. These evolved from the classic A*, which keeps its search directed towards a goal and builds a path in the form of one-way links from square to square.
A* grid paths are limited to orthogonal and/or diagonal lines depending on the game. This is fine if the environment is mostly tight corridors along those lines, or the actor has fixed goals arranged along those lines. But with dynamic and arbitrary goals in open spaces the path can alternate awkwardly between directions.
Theta* addresses that problem. It allows walking straight from any square to any other square if there is an unblocked line of sight. Lazy Theta* is an optimized variant that assumes line of sight first and corrects it later.
Keep the following information about each square:
The procedure:
This can be fine if your grid is not too big.
Before 3.2.1: Check line of sight from Neighbor to Square’s previous. If not blocked, continue processing the Neighbor against Square’s previous, not Square itself.
In 3.2: Process each Neighbor against Square’s previous, not Square itself.
Before 3.1: Check line of sight from Square to its previous. If blocked, find the lowest-cost neighbor of Square that has been queued, then make it Square’s new previous and recalculate Square’s cost.
The classic line-on-grid algorithm is Bresenham. Note this implementation is meant for undirected lines. It requires modification if you need it to start at the first line point.
If you want a fatter line of sight to avoid squeezing between diagonal blocks, you might try a second and even third line of sight offset by one square, or consider adapting Wu’s anti-aliased line algorithm.
Keep track of a Best Goal, that is the closest known square to the Goal. This should be combined with a limit on the number of squares searched, or you will end up searching every reachable square.
Before 3: Set the Best Goal to Start.
After 3.1: If Square is a better goal than Best Goal, set Best Goal to Square.
After 3: Build the Path as in 3.1 but start at Best Goal instead of Goal.
Lazy Theta*: Any-Angle Path Planning and Path Length Analysis in 3D
github.com/zehuilu/Lazy-Theta-with-optimization-any-angle-pathfinding
Did you like this post? Tell us
Leave a comment
Log in with your itch.io account to leave a comment.