Path Finding Algorithm in C ++ game development sixteen games (2): Maze & A * algorithm Basics

Source: Internet
Author: User

This series of articles was compiled by the Central fog of. For reprinted articles, please indicate the source.

 Http://blog.csdn.net/u011371356/article/details/10289253

Author: Sina Weibo, Wuyang:Http://weibo.com/1689160943/profile? Rightmod = 1 & WVR = 5 & mod = personinfo



Because some time ago in learning the Cocos2d-X engine, and then I recently practiced handwriting a small game practice, spent a lot of time, so this series is not updated, but the future fog will continue to update, share new things you have learned.

 

In the previous section, the central fog said that we should first talk about the maze, but at least now, the central fog thinks that the maze is not very useful, so I am not going to elaborate on it. Here is a brief introduction.

In previous games, due to hardware performance, there was no way to afford a wide range of pictures. In addition, in order to reduce the workload of Art personnel, it is worth exploring how to use a few resources to create different games. The answer given by older game programmers is the maze. Using the maze, on the one hand, you can use several walls to randomly generate endless maps using algorithms, so that players can have different experiences. On the other hand, you can greatly extend the length of the game. After all, you don't want to paint games that you bought for dozens of dollars.

 

A problem involved in the maze is the random generation of algorithms.

You can first regard the maze as a square, such

We can specify an entry and an exit, such as a grid in the upper left corner and a grid in the lower right corner.

Now we need to have a connection Entry and Exit. Generally, the route is required to be unique.

If we regard each lattice as a vertex, it is actually a graph. The problem is equivalent to finding the Spanning Tree of this graph. We can use algorithms that involve spanning trees, such as BFS, DFS, Prim, and Kruskal.

If you are interested, read this article.

Various random Maze generation algorithms

Excellent explanation

It also mentions a clever method, recursive segmentation, which is very clever. You can check it out.

In addition, when learning the data structure, we learned and checked the set, which is also called not intersection. We can also get a good algorithm by using and querying the set. At first, each grid is a set, and then a random wall is selected to remove it. The grids on both sides of the wall are connected and merged into the set where the two are located, determine whether the Exit and Entry grids are in the same set. If not, repeat the wall splitting and merging process.

 

Another problem with the maze is the pathfinding algorithm.

Of course, high-end algorithms are applicable, but generally the maze is only used to find a path. The common DFS and BFS can be easily understood and implemented.

 

Today, I want to talk about the recently learned a * Algorithm in Wuyang, which is also an algorithm scum. However, recently, I feel the importance of finding a path algorithm, so I have to study it carefully, there are a lot of A * algorithms written on the Internet, all of which are very good. The following are the understandings of smog, hoping to be as popular as possible.

 

Recently, Yunyang wrote a small game. The general map is as follows:

You can see that the joystick is used to control the movement. The fog center transplanted the game to Android, so you will surely feel it hurts, so how can you control the small screen! In fact, Wuyang doesn't want to do this either. Isn't it good to directly click which character to go? But it involves a problem: finding the path! There are obstacles on the map. After clicking a place, the character must go over in the shortest path. This is the first android game in Wuyang learning, and I didn't want to find any trouble for myself, therefore, an egg joystick is used.

If you are interested in using Android, try it out. For details, please click this article.Click me..

 

The above is actually to say that the pathfinding algorithm is very important. Today we only talk about the most basic a * algorithm, and there is no variation.

Due to the importance and foundation of a * algorithm, there are many researches on a * algorithm on the Internet. Many great gods have discussed and extended them. However, many of them are not suitable for beginners. the following link is the most basic and clear description of the * algorithm. Good English friends can directly read this article.

The most understandable a * Algorithm

If you are not good at English, you can also read this Chinese version. The translation is still good and everyone should be able to understand it.

A * algorithm Chinese translation version

Let's take a look at the above two articles. The following is the understanding of the smog learning algorithm, and I will describe it again to deepen my understanding of it, I hope it can be more popular. If you are interested, you can refer to it.

 

Problems and objectives

First, let's clarify the problem to be solved. Let's look at the figure below.

This figure represents our map. For convenience, we use squares instead. Of course, it can also be a polygon.

The green square indicates the start point, the Red Square indicates the end point, and the blue square indicates the obstacle.

Our goal is to find a shortest path from the green square to the Red Square.


Path Evaluation Scheme

We may encounter many choices when looking for a path. What is the best? This requires us to make a judgment and a standard. The formula we use is

F = G + H

The following describes the meaning of this formula.

F indicates the length of the path. We want to find the path with the minimum F value.

We need to go through a series of squares to reach the destination point from start a when looking for a path. For example, if we arrive at a certain node M point halfway, the path from start a to node m is known, the G value here.

However, the process from m to B is such a path-finding problem. We don't know its length, so we need to use an estimation algorithm, and its estimation value is H.

 

The rest will be written again next time. It will be sleep later.

 

In addition, the school is about to begin. Recently, WuYang is very busy, because WuYang is also trying to make its own games and is constantly learning, so the update is not as timely as it was in January. This blog series is mainly used to share things learned from the fog center. You also need to think about how to implement things for beginner games. In fact, these things are enough for everyone to make their own games, it is recommended that you try it. The seemingly complex game logic can be written and assembled slowly, and the architecture maintainability will be considered later. First, implement the function and consider the problem of improvement and optimization. On Weibo, there were several friends who asked about the process of learning from the central fog. In fact, the central fog is now a beginner, and everything that has been learned from the central fog has been written in this article, if you are interested, you can check it out and hope it will be of reference significance to you,Something that has been learned by WuYangIn fact, it is more important to practice it by yourself. Writing a few games will grow slowly.

 

 

 

 

 

 

 

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.