Finding a Way Among Round Obstacles
A * Path Finder Algorithm is a powerful tool for quickly generating optimal paths. Usually A * is shown when navigating maps from grids, but it can be used not only for grids! It can work with any graphs. You can use A * to find your way in the world of round obstacles.
In the original article, all images are interactive.
How does one algorithm solve both of these problems? Let's start with a brief description of how A * works.
Algorithm A *
Algorithm A * finds the optimal path from the start to the end point, avoiding obstacles along the way. He realizes this, gradually expanding many partial paths . Each partial path is a series of steps from the starting point to some intermediate point on the road to the goal. In the process of working A *, partial paths are getting closer to the end point. The algorithm stops working when it finds the full path, which is better than the remaining options, and this can be proved.
At each step of the algorithm, A * evaluates the set of partial paths and generates new paths, expanding the most promising path out of the set. To do this, A * stores partial paths in a priority queue, sorted by approximate length- the true measured path length plus the approximate remaining distance to the target. This approximation should be underestimated ; that is, the approximation may be less than the true distance, but not greater than it. In most path finding tasks, a good understated estimate is the geometric distance in a straight line from the end of the partial path to the end point. The true best path to the goal from the end of the partial path can be longer than this straight line distance, but cannot be shorter.
When A * starts, the priority queue contains only one partial path: the starting point. The algorithm repeatedly removes the most promising path from the priority queue, that is, the path with the smallest approximate length. If this path ends at the endpoint, then the algorithm has completed the task - the priority queue ensures that no other path can be better. Otherwise, starting from the end of the partial path that he removed from the queue, A * generates several more new paths, taking unit steps in all possible directions. He puts these new paths back into the priority queue and begins the process again.
A * works with a graph : a set of nodes connected by edges . In a grid-based world, each node denotes a separate mesh cell, and each edge is a connection to neighboring cells to the north, south, east, or west.
Before we can run A * for the forest from round obstacles, we need to convert it to a graph. All paths through the forest consist of alternating segments of lines and arcs. They are the edges of the path graph. The endpoints of these edges become nodes.
A path through a graph is a series of nodes connected by edges:
Both segments and arcs are edges of the graph. We will call the segments the transition edges , because the path uses them to move between obstacles. We call arcs the enveloping edges , because their task along the way is to go around the sides of the obstacles.
Next, we will explore a simple way to transform a forest with obstacles into a graph: we will generate all possible transition edges and envelopes of the edges.
Transition Edge Generation
The edges of the transition between a pair of circles are line segments that barely touch both circles; such segments are called tangents to two points , and for each pair of circles there are four such tangents. Tangents to two points that intersect between the circles are called internal tangents to two points , and those that pass outside are called external .
Inner tangents to two points
In the past, the search for internal tangents was important for calculating the length of the belt connecting two pulleys of different sizes, so the task of creating internal tangents to two points is called the problem of belts . To find the internal tangents to two points, you need to calculate the angle in the drawing below.
As it turned out, for circles with centers and and radii and whose centers are at a distance :
Defining we can easily find the points , , and .
External tangents to two points
When constructing external tangents (this is the task of pulleys ), a similar technique is used.
For external tangents we can find in the following way:
It doesn't matter which of the circles is bigger, A or B, but as you can see from the picture, laid on side A closest to B, but on side B farthest from A.
Line of sight
Taken together, the internal and external tangents to two points between the two circles form the edges of the transition between the circles. But what if one or more transition edges closes the third circle?
If the transition edge is overlapped by another circle, then we need to discard this edge. To recognize such a case, we use a simple calculation of the distance between a point and a line . If the distance from the transition edge to the center of the obstacle is less than the radius of the obstacle, then the obstacle overlaps the transition edge, so we must discard it.
To calculate the distance from a point to a straight line segment we use the following method :
First, we calculate - part of the distance along the segment where the perpendicular intersects the point :
Then we calculate the position on the :
Distance from to cut Is the distance from before :
Because , the circle overlaps the line of sight of in , and the edge must be discarded. Ifthen the line of sight of in exists and the rib should be left.
Edge Envelope Generation
The nodes of the graph connect the transition edge with the envelope edge. In the previous sections, we generated transition edges. To generate the envelopes of the edges, we start from the end point of the transition edge, go around the circle and end at the end point of another transition edge.
To find the set of envelopes of the edges of a circle, we first find all the edges of the transition that are tangent to the circle. Then create the envelopes of the edges between all the end points of the transition edges on the circle.
Putting it all together
Having generated transition edges, envelopes of edges and nodes, and then discarding all overlapped transition edges, we can create a graph and start searching for the path using the A * algorithm.
The graph generation procedure we examined works well enough to explain the algorithm, but can be improved in many aspects. Such improvements will allow the algorithm to spend less CPU and memory resources, as well as handle more situations. Let's look at some of the situations.
Obstacles that concern each other
You may have noticed that in the examples shown above, round obstacles did not overlap and did not even touch. Assuming that the circles can touch each other, this complicates the task of finding the path
Two point tangents
Recall that tangents to two points can be found using this formula for internal tangents:
and formulas for external tangents:
When two circles touch each other or intersect, then there are no internal tangents between them. In this casemore than one. Since the value of the arccosine is outside the domain of definitionnot defined, it is important to check the intersection of circles before finding the arccosine.
Similarly, if one circle is completely located in another, then there will be no external tangents between them. In this case out of range , that is, it does not have an arccosine.
Line of transition edge visibility
If we allow the possibility of touching or crossing obstacles, then new cases arise in the calculations of the line of sight of transition edges.
Recall the calculation- part of the distance along the edge of the transition at which the perpendicular to the point touches the edge. If touching the circles is not allowed, then the value out of range , that is, the circle cannot touch the edge, because for this it would have to touch one of the end points of the edge. This is not possible because the endpoints of an edge are already tangent to other circles.
However, if we allow the possibility of overlapping circles, then the values out of range can overlap the line of sight along the edge. This corresponds to cases where the circle is located beyond the end of the transition edge, but overlaps or touches the end point. To track such cases mathematically, we will limit interval :
Envelope line of sight
When obstacles can touch each other or intersect, the envelopes of the edges can be blocked by obstacles in the same way as the transition edges. Consider the envelope edge from the figure below. If the envelope of the rib touches another obstacle, then the rib is blocked and must be discarded.
To determine if the envelope of the edge is blocked by another obstacle, we use the following method to determine the points at which two circles intersect. If circles have centers at points and and radii and where - distance between and , for starters, we need to check several cases. If the circles do not touch each other (i.e.),
are one inside the other () or match ( and ), then circles cannot affect each other's envelopes.
If not one of these conditions is met, then the circles intersect at two points; if the circles touch each other, then these two points coincide. Consider the radical axis connecting the intersection points; it is perpendicular to the line connecting and at some point . We can calculate the distance from before in the following way:
Finding we can find the angle :
If is zero, then the circles touch at . Otherwise, there are two intersection points corresponding to positive and negative.
Next, we determine whether any of the intersection points is between the start and end points of the envelope of the edge. If so, then the obstacle blocks the envelope edge, and we must discard this edge. Note that we do not need to consider the case when the envelope edge is entirely inside the obstacle, because the line of sight cutoff for the transition edges has already thrown off this edge.
After making changes to the calculation of the tangents to two points and the line of sight for the transition edges and the envelopes of the edges, everything works correctly.
Variable actor radius and Minkowski extension
When calculating the navigation of a round object in the world of circular obstacles, one can take into account observations that simplify the solution of the problem. First, one can simplify the work by noting that the movement of a circle of radius r through the forest is similar to the movement of a point through the same forest with a single change: the radius of each obstacle increases by r . This is an extremely simple case of applying the Minkowski sum . If the radius of the actor is greater than zero, then before starting, we simply increase the size of the obstacles.
Deferred Edge Generation
In general, a graph for a forest from obstacles contains transition edges, but since each of them needs to be checked for line of sight with obstacles, then the total graph generation time is . In addition, pairs of transition edges can lead to the creation of envelope edges, and each of them must be checked with each obstacle for the line of sight. However, due to the high efficiency of the A * algorithm, it usually looks at only part of this large graph to create the optimal path.
We can save time by generating small parts of the graph on the fly during the execution of the A * algorithm, rather than doing all the work in advance. If A * finds the path quickly, then we will generate only a small part of the graph. We do this by moving edge generation to a function
There are several cases. At the beginning of the algorithm, we need neighbors of the starting point. These are the edges of the transition from the starting point to the left and right edges of each obstacle.
The next case is when A * just got to the point on the edge of an obstacle along the transition edge:
neighbors()should return the envelopes of the edges leading from. To do this, we determine which transition edges go out of the obstacle by calculating the tangents to two points betweenand all other obstacles, discarding all those that are not in line of sight. Then we find all the envelopes of the edges connectingwith these transition edges, dropping those that are blocked by other obstacles. We return all these envelopes of the edges, saving the transition edges for return in a subsequent call
The last case is when A * went around the envelope edge along the obstacle and he needs to leave along the edge of the transition. Since the previous step calculated and saved all the transition edges, you can simply find and return the correct set of edges.
We cut off the pointed envelopes of the ribs
Envelope ribs connect transition edges touching one circle, but it turns out that many of these envelope ribs are not suitable for use in an optimal way. We can speed up the algorithm by eliminating them.
The optimal path through the forest of obstacles always consists of alternating transition edges and envelopes of edges. Suppose we enter a node and decide how to get out of it:
Login through means we are moving clockwise () We must exit through the node, which allows us to continue to move clockwise (), that is, we can only exit through the node or . If you exit throughthen an inflection () ways that will never be optimal. We need to filter out such pointed edges.
First, note that A * processes each non-oriented edge anyway. like two oriented edges and . We can take advantage of this by marking the edges and nodes with orientation.
- Knots become nodes with orientation - clockwise () or counterclockwise () arrows.
- Unoriented transition edges become oriented edges and where and Are orientations, and means the opposite direction .
- Unoriented Rib Envelopes become oriented edges and . This is where filtering occurs: we do not enable and , because a change in direction creates kinks ()
In our circuit, the node will turn into two nodes, and it has an incoming transition edge and outgoing transition edge . If we hit the road throughthen should exit through the node which will be either a transition edge (through the envelope edge ), or transition edge (through the envelope edge ) We can not get out through, because the direction of rotation will change this way, and we filtered the envelope of the edge .
By filtering out these pointed envelopes of edges from the graph, we increased the efficiency of the algorithm.
Cutting off intersecting edges
Partial paths can be cut off, the last edges of the transition of which intersect the penultimate edge of the transition.
See Game Programming Gems 2 , Chapter 3.10, Optimizing Points-of-Visibility Pathfinding, written by Thomas Young. This chapter discusses clipping nodes, but not for circles, but for polygons.