Detailed explanation of ARPG map model quadtree construction (1)

I have nothing to do recently, and I plan to write something. I am the most famous programmer who has been in the game development industry for many years. I have learned from the past and learned more. It is a meaningful thing. Many program students have written logic code for many years, and there is not much opportunity to realize the underlying requirements, because the core framework of most projects has been built at the beginning of the project. Part of the code should not be thought (some of the bottom layers are not too perfect) to be maintained and organized by the main process. It is difficult for programmers to make breakthroughs if they do not understand the role and use of knowledge points in practice. It’s like I want to become a martial arts master every day. It’s useless if I can’t find someone to fight at home every day. Not much nonsense. The content of this series will not be much, and there is no time requirement. I don’t know when It's over, but I'll do it as soon as possible. The content of each part may be a lot or a little, but it's all what I realize and think. If you can gain something from it, it's an honor!

Quadtree is a search optimization algorithm. What is search optimization? Let’s talk about search first, what to search for?

In large-scale ARPG games, as we all know, you will see a lot of monsters, players, NPCs, scene special effects, etc. After bearing the pressure of rendering on the same screen, it is necessary to design at the interaction level. What is interaction?

The interaction is easy to understand, that is, to communicate with each other. When I am bored, I don't find an NPC crush to chat. I put the mouse on the MM and she will smile at me happily. When I click on her, she will pop up a panel. Say hello to me and so on. Another example is that I got a weapon and wanted to feel the power of the new equipment. I went to the southern suburbs and killed a few hares. When I clicked on the hare, it would be selected by me and tried to Escape, but it's not that easy, I will try to approach it, kill it quickly, the game experience with this kind of interaction will be very good, but some interactions are not so simple, and some complex interactions look like ?

If you have played legendary games or MMO games, it will be easy to find out. When you enter the game and stand in the main city, you will find a sea of ​​people, and you will find that the former NPC crush has been buried in the crowd and cannot be found. But when you move the mouse over and click on it, you will be surprised to find that although you don't seem to have clicked on her, a dialog box pops up to say hello to you. This is a complex interaction situation. What does this have to do with search optimization? Woolen cloth?

If you analyze and think from the perspective of a designer, you will find that if I want to know whether the current mouse click is an NPC crush, how can I achieve it?
The simplest idea from the program level is to traverse all the display object instances in the field of view, according to the instance type and whether the rectangular range of the instance contains the mouse point, this seems to be achievable, but if there are many players in the field of view, as many as you are in During the traversal process, the game is stuck because the code runs for too long, causing the game to drop frames. At this time, search optimization comes in handy.

The quadtree algorithm is commonly used in search optimization in 2D games. It can solve the above performance problems very well, thinking that we do not need to consider the number of objects, but only need to traverse the quadtree nodes under a certain range around the mouse point. All the display objects can be displayed, which may be a bit difficult to understand. I will explain it slowly in the future. In order to better explain how to complete the optimization of the search, let's first try to create a map based on a certain size and the minimum rectangle. Ranged quadtree.

Now we have prepared a 15000*15000 pixel map, which is full of many monsters, flowers and plants, NPC crush and some treasure chests. We set the minimum rectangle width and height of the quadtree node to 250 pixels by default ( The default width and height are the same rectangle), let's calculate a value, the depth of the tree (depth)

The depth of the tree is how many layers of nodes the tree has, and it can also be simply understood as the number of layers of the tree

This calculation is very simple. First of all, we assume that there are n layers, then the value of the multiplication of the number of nodes m of the nth layer and the width of the smallest rectangle must be greater than or equal to the width of the map,
or the number of nodes m of the nth layer and the smallest rectangle. The value of the height multiplied must be greater than or equal to the height of the map, which is easy to understand, but there must be a trade-off. Take the maximum value of the width and height of the map as the comparison object, so that the entire map area can be covered, and there will be no missing areas. Okay , the following is to use the code to achieve

Understand the above content, the code is very simple to implement

//Parameters: map width, map height, minimum rectangle width or height
function getDepth(mapW,mapH,minR):int
{
var value:int;
var mapSize:int = Math.max(mapW,mapH);
var n:int =1;
while(1){
value = Math.round(mapSize/Math.pow(2,n));
if(value<=minR)return n;
n++;
}
return -1;
}

How to understand it?
It's very simple, just divide 15000 by 250, and add one to n every time you divide it. When the divided value is less than or equal to 250, the n obtained is the depth we want. The final calculation of n=6
is very good, and we will create it later. A quadtree of depth 6

note: ARPG games are all based on grids, so when creating a quad tree, the minimum rectangle reference range is 200-400 pixels.

Due to time constraints, I will write it here today. Please look forward to seeing what will happen next! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324710038&siteId=291194637