A * pathfinding path optimization

Source: Internet
Author: User
Tags abs

The purpose of this paper is to make some humanized adjustments to the path generated by a * pathfinding algorithm, so that it does not appear to be too mechanized. On the principle and implementation of a * algorithm, readers can read other materials, which are not elaborated here. How to write a valuation function

A * pathfinding algorithm is essentially a directional breadth-first search algorithm that uses a valuation function to estimate the shortest possible path, and, after each search iteration is completed, selects the optimal one (that is, the closest point from the endpoint) of its adjacency point as the starting point for the next iteration. So repeat until you find the end. The general wording of the valuation function is listed below:

The value of set I to the starting point is S, and the total valuation of e,i points to the end point is equal to S+e. The value of S is determined:

S = parent. S + 1 (the I point is the adjacency point in the horizontal or vertical direction of its parent node)
or
s = parent. S + sqrt (2)) (I point is the adjacency point in the oblique direction of the parent node)

The value of the e point needs to be estimated. A precise notation:

Horizontal distance: DX = ABS (IX-EX)
vertical distance: DY = ABS (Iy-ey)
distance to be traversed: v1 = min (dx, dy) * sqrt (2)
distance to be traversed: v2 = max (dx, DY) -min (dx, dy)
E =  V1 + v2

A rough notation:

E = ABS (IX-EX) + ABS (Iy-ey)

how to avoid steering jitter

The result of a A * pathfinding is optimal, but not unique, because the closest route between two points may be more than one. Then the problem arises, which is better when two of the best routes are equally spaced.


(Red is barrier, white is passable, black is search path)

As shown in the figure above, is a * 8 direction of the search to get two of the same route, but the left of the route in the middle position of the "turn", more than the right side of the route to a "corner." If there are too many turns on the route, the character walks in the process of frequent turning, resulting in "jitter" phenomenon. Therefore, we decide that the right route is better than the left-hand route. To solve this problem, we can choose to "turn" fewer routes by modifying the valuation function.

The turning problem can be simplified to go in one direction as much as possible before considering turning. Further simplification, the closer to the beginning or end point, the higher the priority. We add a disturbance value to E, factor,

Factor = min (abs (IX-SX), ABS (IX-EX)) + min (abs (IY-SY), ABS (Iy-ey))
factor *= 0.01

Factor value can not be too large, otherwise it will result in the search results is not the shortest distance, so appropriate to the factor by the previous scaling factor. how to stay away from obstacles

A * search for the effect is a shortcut, take the shortest way. But for the game experience, this is not entirely a good thing, the Broad road does not go, must take the cliff, accidentally dropped Abyss, or stuck on the edge of the rock. So, how do we avoid these phenomena? Again, we can do this by modifying the estimate function.

We add a disturbance value to each of the accessible areas, and the closer the barrier is to the accessible area, the greater the disturbance value. Calculation method of interference value:

Factor = 0 for
(x =-N; x <= n; ++x)
{for
      (y =-n; y <= n; ++y)
     {
        if (isobstacle (ix + x, iy + y)) c5/>{
            Factor + = N-min (ABS (x), ABS (Y))}}}

We can even increase the disturbance value according to the material of the surface, such as the Mountain Road and swamp belt is obviously more disturbing than the road. PostScript

In short, we can adjust the valuation function to achieve different effects. However, can not be arbitrarily modified, bad valuation function, will increase the search cost.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.