Cocos2d-x game instances-astar algorithms (2)

Source: Internet
Author: User

Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man

In the previous article, we studied the basic concepts of the astar algorithm. This article introduces the specific implementation of the astar algorithm.

First, the function starts from the findpath function. The initialization content is as follows:

First, assign the current location and target location to the member variables of this class, then pass our map variables to the member variable map, and initialize the open, close, and path lists respectively, the path and close lists are empty first, and the open list is convenient for heap sorting (the index is multiplied by two or 0 when the index starts from 0 During Heap sorting, which causes inconvenience ). Then add the start point to the open list. The geth estimation function is involved here, because the cost to the start point is 0. The estimated function geth from the start point to the end is as follows:

The estimation function we selected is relatively simple, that is, calculate the vertical and horizontal index values from the current point to the target point, multiply it by 10, and we set the price between neighboring cells to 10, the cost of the diagonal line is 14 (10 * Root 2 ). Go back to findpath and start searching.

We traverse the elements in the open list. First, we use the fromopentoclose () function to retrieve the first element in the open list (the first element is invalid, as mentioned above) delete and add it to the close list. The fromopentoclose function is as follows:

First, add the first element from open to the close function, and then replace the first function in the open list with the last function in the open function in the removefromopen function, next, perform the heap sorting. Here we will briefly talk about the heap sorting, that is, regard the entire array as a tree structure, and then ensure that the parent node is smaller than the F value of the child node. In this way, the first element is the best

If there are any errors, I hope you can correct them more.

Next, continue to the astar algorithm game instance

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.