Reference article:
Http://games.sina.com.cn/zl/duanpian/2014-03-11/105973.shtml
http://www.oschina.net/translate/understanding-steering-behaviors-collision-avoidance?cmp
http://blog.csdn.net/ityuany/article/details/5509750
I. Definition of artificial intelligence
Artificial intelligence (AI, Artificial Intelligent), refers to the use of algorithmic programming to enable the computer to imitate people to accomplish some like human tasks, while performing tasks in imitation of human thought and wisdom, and even through a large number of learning training to accumulate learning experience, improve their own problem-solving wisdom and efficiency. Artificial application of a wide range, such as intelligent transportation, artificial intelligent robot, smart home appliances, etc., by imitating human intelligence instead of humans to do some repetitive work. Artificial intelligence is still in the artificial stage, that is, the computer's intelligence is still under the human manual controllable predictable range of operation, artificial intelligence is actually in a large number of logical operations and a large number of data input processing on the basis of implementation, artificial intelligence needs a lot of data input training to make it more intelligent. The typical recent Google Ai Alpha Dog has done thousands of chess game training to defeat the world go champion Li Shishi. Unlike human intelligence, AI relies on its own powerful computational speed to learn and intelligently, while human intelligence can understand more about things through a small amount of learning. In the current human understanding of their own intelligence is still not sufficient, artificial intelligence still relies on artificial processing.
Two. Ai in the game
In the game, especially the virtual reality game to create a virtual world as real as possible, the virtual world needs to set up a lot of NPC (Non-player Character) characters interacting with the player, which requires NPC characters with intelligence and the player more natural and real communication, to help players play a smooth game To improve the gaming authenticity experience. NPC's Intelligent action is a series of finite state sets, and its coherent state changes to achieve interaction with the player. NPCs and players together form the role group in the game.
On the other hand, one of the core elements of AI technology in the game is the intelligent pathfinding of the characters in the game, the characters can avoid obstacles like people to choose a reasonable path from the starting point to reach the destination, or guide the player to the designated location in the virtual scene. The search algorithm in game is mainly divided into two kinds of blind searching and heuristic search, which is connected with the shortest path problem in graph theory, and realized on the data structure of graph theory. The most basic and mature game pathfinding algorithm is a start heuristic function pathfinding algorithm, there are a large number of RPG role games and strategic games, such as intelligent pathfinding are used to based on the A-star algorithm or its variant optimization algorithm, and other algorithms combined with the hybrid algorithm. In addition, the authors use the potential principle in physics to design an automatic negative charge searching algorithm based on the electric potential matrix, which is a new and creative way of thinking. Depth-First search algorithm (DFS) and breadth-first search algorithm in graph theory and dijksrta single-source shortest path greedy algorithm are typical blind search algorithms. In addition to the above-mentioned goal-specific path pathfinding, a random pathfinding algorithm is also needed, which is mainly applied to the blind loafe of monsters in a certain area, making the monsters appear natural and dynamic.
2.1 Finite state machine
An important application of the finite state machine (FSM, finitestate machines) in unity is to control the animation system continuity transitions for a character:
The finite state maneuver is a collection of characters of a game character, which is divided into a set of finite states, and a series of successive actions are produced by the transformation of the condition triggering state, the direction arrows between States represent the direction of state transition and the conditions of conversion, and the simplest finite state maneuver picture system is shown. The continuous transformation of the action state, such as resting, walking and jumping, is triggered by the condition. The limited state maneuver picture has the most application in the game NPC, but the limited state maneuver picture's AI performance is poor, the movement limited is easy to be predicted by the player and appears blunt, the realism is largely limited.
2.2 Path-finding navigation map
In the game to seek the road before the need to be feasible in the terrain and the obstacles can not walk the area of the separation, with the feasible to walk the region of different methods of segmentation, split into nodes so that can be transformed into algorithms and data structures to be processed objects, walking area of the division has the following types:
1. Delete a grid navigation map
Similar to raster images, the map is segmented with equal spacing squares, represented as a raster matrix, each raster represents a location point, and the point as a value of the matrix can be weighted to indicate whether the point is an obstacle, if not the obstacle is what different terrain and so on. Rasterized navigation charts are more granular and accurate than other types of navigational diagrams, and they consume a lot of memory.
2. Polygon Navigation diagram
Polygon navigation diagram is to divide the navigable terrain with a convex polygon to split the fill, the principle is that the adjacent two polygons can be directly through the barrier-free. Different polygons can be selected for different terrains.
3. Viewpoint Navigation Chart
With a series of representative key points to comprehensively cover the core frame of the walking terrain, so that the terrain is greatly simplified, the search space of the map is reduced to a large extent, which is helpful to improve the search efficiency, but the path generated in this map will cause the character to go "Z" glyph defects. The waypoint Pathfinding method is based on this kind of navigation map.
Three. Game AI pathfinding algorithm3.1 A star pathfinding algorithm
A star algorithm, also known as a * algorithm, is a heuristic function path calculation search algorithm, through the design of reasonable heuristic function can greatly reduce the amount of computation in the pathfinding process, improve the computational efficiency, and the estimation is not accurate is the characteristics of the heuristic function, so use a * The path of algorithm calculation may not be the best path that people understand, but a * can efficiently provide a relatively reasonable path in the game, which is widely used in game pathfinding.
1. Node (node) in a *
Nodes are the basic unit of a * algorithm calculation. As a * algorithm can be applied to a variety of navigation charts, in different navigation map, the representation of the nodes are not the same, in the polygon navigation map, a polygon is a a * node, can be view of the navigation map of each can be a a * node, the deletion of each rectangular grid navigation map is a * node.
2. Valuation function
The evaluation function is used to evaluate the probability of a node being chosen as a path point, which uses the information of the location relationship between the end point itself and the starting point, the heuristic algorithm searches the more reasonable path, the path is the set sequence of adjacent nodes, and the evaluation function expression is as follows:
F (n) = G (n) + H (n)
The f (n) in the formula represents the estimated value of the nth node, G (n) represents the distance value of a node according to a distance rule to a starting point, and H (n) represents the distance value of the node according to a distance rule to the end point, so the smaller the estimated value f (n), the less the walk consumption represents the path, the greater the probability of being selected as the path point.
Distance calculation rules are mainly Manhattan distance, Euclidean distance and diagonal distance of three kinds. Manhattan distance refers to the sum of horizontal distance and vertical distance between two points, the Euclidean distance refers to the geometric distance between two points, and the diagonal distance refers to the larger of the horizontal distance and vertical distance.
In the right triangle formed between P1 and P2 two points, it is assumed that the horizontal right angle side length is a, the vertical right angle side length is B, the hypotenuse distance is C, then:
Manhattan Distance: D1 = A + B
Euclidean distance: D2 = C
Diagonal Distance: D3 = max{a,b}
In the practical application of the algorithm, according to the research of [13] Shen Jian, Euclidean distance is better in the calculation efficiency of pathfinding, and it is often used in mixed applications to improve the computational efficiency and the adaptability of the algorithm according to different situations.
3. A * algorithm execution process
The algorithm maintains the open table and the closed table two tables, which store the possible nodes to be visited, which are continuously added to the evaluated nodes.
(1) The algorithm initially adds the starting point to the closed table, its valuation function value: F (0) =h (0), where g (0) = 0, for its distance to itself.
(2) Then add all nodes adjacent to the current point (which is temporarily the starting point) to the Open table, if you have joined, do not operate. On the basis of the value of the current node valuation function, accumulate the F, G, h values of all adjacent nodes, and if the new value of the nodes in the closed table is smaller than the previous, then update to a smaller value, select the node with the lowest F value as the new path point, and set it to the child node of the current node. The new path point continues to traverse adjacent nodes as the current node.
(3) Repeat (2) operation when the endpoint joins the closed table, the algorithm ends, the final path is obtained based on the parent-child node relationship, and the search fails if the open table is empty and the end point is still not in the closed table.
(4) The above algorithm execution process obstacle node does not join the Open table calculation.
The simplest application of a * algorithm is in the classic brick map (Tile), each square as a node, from the current grid node can go up, down, left, right, top left, right, left, bottom right, eight to the next adjacent node, the actual development of the need to consider the size of the node element, that is, the density of the map grid The classic Battle of tanks game is based on the brick map. The squares in the map are divided into feasible walking (moveable) and non-walking (not moveable), and the non-walking node algorithm ignores skipping and does not join the calculation. In the brick map, for the sake of simple calculation, the calculation of Euclidean distance is used, the calculation of which is in the Manhattan distance.
4. Inflection point method to optimize a * path
The node of the algorithm is the visual key in the view-point navigation graph. The benefit of setting the node is that the nodes are set up freely, can be weighed down by the number of a * node, reduce the amount of computation, or can be adjusted quickly according to the designer's requirements. The problem is that in a geodetic map, if you set the node to be sparse, the resulting path will appear stiff and inflexible, resulting in an unnatural visual experience. The above problems can be largely avoided in the navigation grid (Navigation mesh, also known as Navmesh), which is the polygon navigation diagram.
In polygon navigation diagram, using convex polygon as node, on one hand can greatly reduce the number of nodes, on the other hand can better cover the entire terrain. In addition, by calculating the position relationship between the line and the polygon navigation grid adjacent points between the start point and the end point, you can get a direct barrier-free pass, and based on this optimization, you can avoid the reentry "Z" route as the visual navigation point search Path, which causes the role to make extra detour.
The path to search in Navmesh using a * algorithm is a series of adjacent polygons, in fact, in many cases, the walking area of multiple polygons can be directly through, at this time, these polygons can be merged to form a straight path, only the inflection point must turn to make the path transition, The merging of polygons and the way to find inflection points are as follows.
According to the Polygon navigation chart generation specifications, there is only one common edge between the adjacent two polygons, the common edge has two may be the end point, from one polygon to another two endpoints can be recorded as the left endpoint, the right end point, the node to two end of the direction vector recorded as the left vector vleft, right vector vright. The left and right vectors can be calculated according to the coordinates of the polygon nodes and the two endpoints, and a target direction vector VD can be computed according to the destination coordinates, so a node data structure can be designed as follows:
The algorithm executes the following procedure:
(1) The algorithm maintains a inflection table p, which indicates the key point of the optimized path, as the result of the algorithm;
(2) The algorithm initially sets the representation point of the start node to the center point of the polygon, and joins the inflection table p, then calculates the angle between the left and right vectors of the next node and the destination direction vector, and the end point in the smaller direction is the representative point of the next node;
(3) Calculate the left and right vectors according to the next node's representation and left and right endpoints, if the vectors are nonzero, execute and
The starting node is the same operation, if the left vector is zero, the representation point and the left endpoint coincide, and the left end points as a inflection point to join the inflection table p;
(4) Until the end of the node is reached, the current representation point and the end point successively joined the inflection table p, the algorithm ends.
A * path through the inflection point optimization will appear along the boundary of the navigation map, as long as the set of the game character's own radius as offset correction can be achieved with the navigation map boundaries to maintain a certain distance, to avoid the collision with the boundary lag.
3.2 Potential matrix pathfinding algorithm
The potential matrix pathfinding algorithm uses the moving object, the navagent in unity as a negative charge, to simulate the movement of the negative charge to the potential high in the potential field, by changing the potential field to guide the negative charge along the potential field line to the destination path. Setting method based on electric potential field this algorithm is best applied in a rasterized navigation map to more accurately describe the details of the potential field, and the algorithm uses a map matrix to store the potential integer value of each point of the terrain.
The method of setting the destination point is to establish a new goal matrix to set the potential of the point of destination to a maximum value, and then a circle or a rectangle to the surrounding to spread out, the farther away from the point of the potential value of the smaller, until 0, and then the destination matrix and the original map matrix to change the potential distribution, Thus, the negative charge moves along the electric potential field to the point of destination.
This algorithm is essentially a greedy algorithm (greedyalgorithm), the disadvantage is not guaranteed to find the correct path, it is possible to calculate no solution. This algorithm can be used in some relatively regular and simple terrain to complete the Pathfinding calculation task.
3.3 Group Behavior (flocking Behavior) pathfinding
Some games will require the common pathfinding function of the role group, if each individual is carried out alone to find the road, it will cause the computational volume expansion, resulting in a large number of unnecessary duplication. Therefore, the movement of the character unit can be implemented by using behavior control (Steeringbehavior), that is, a role unit in the selection group as the leader of the group (Leader), alignment using the pathfinding algorithm for the pathfinding calculation, Then the other group members follow a certain flocking rule to move the leader.
The design of flocking rules mainly considers the following three aspects:
(1) Consistency:
That is, the movement direction of the group members should be consistent with the overall direction of movement (this is the direction of leader movement);
(2) Separation:
That is, each group member needs to maintain a certain distance from the other members, avoid coincident together, this can refer to Sterringbehavior collision Avoidance (collision avoidance) algorithm to similar implementation;
(3) Polymerization:
That is, the group members to the overall group members of the average position close, so that the overall search for the group of the effect more realistic.
3.4 Collision Avoidance (collision avoidance)collision avoidance can be used for simple terrain random pathfinding, its performance is very poor, mainly for obstacle avoidance. The logic of implementation is to maintain two vectors: a moving direction vector that represents the current moving direction and moving speed of the character object, and a collision detection vector VC that represents the direction and distance of the obstacle closest to it ;
Medium green squares for the game characters, red squares for the destination, the moving direction of the vector VD is constantly updated according to the role's current position and destination location, so that it always tends to point to the destination, collision detection vector for the role position points to the nearest obstacle vector, the modulus of the vector is the distance to the obstacle, When the distance is less than a certain value, start to avoid obstacles, the method of avoidance is to move the direction vector and collision detection vector inverse VC ' add to get corrected moving direction vector VD ':
VD ' = VD + Vc '
VD = Vd '
The moving direction vector of the corrected instantaneous character is set to Vd ', which bypasses the current nearest obstacle, and then the update adjustment continues to move to the destination.
3.5 Depth-First search algorithm (DFS) and breadth-first search algorithm (BFS)
The graph theory algorithm stores the path information between the map nodes and nodes with a forward or non-reciprocal adjacency matrix, the corresponding value of two points connected in the adjacency matrix is 1, the corresponding value is 0, and the same graph can have multiple depth precedence sequences and breadth precedence sequences. In the path search, the corresponding value of two adjacent points can be set to the distance weight value, and the shortest Path branch is selected after the algorithm search, thus determining the optimal path sequence of an algorithm.
Depth-First search (DFS, Depth first search), and breadth-priority search (BFS, breadth, search) are blind searches that follow certain rules.
The logic rule for depth-first search is to start from the starting point, looking for access to the next connected node, until there is no unreachable connected node, back to the starting point and continue looking for deep connected paths from other branches until all nodes are accessed.
Assuming the starting point of the V1 graph in Figure 3-9 is the first, there are several depth precedence sequences:
V1, V3, V5, V4, V2;
V1, V4, V5, V3;
V1, V4, V2;
V1, V2, V4, V5, V3;
The shortest path is selected according to the definite target node and the weights of the connection and the interception from the sequence.
The search logic for breadth-first search is to start from the starting point, traverse all connected nodes and set as the sub-nodes of the starting point, and then perform the same recursive operation on the child nodes until all nodes are joined in the tree with the starting point as the root node.
Assuming that the starting point of the graph in Figure 3-10 is still V1, one of the breadth-first search sequences is:
V1, V3, V4, V2, V5
The two breadth-first search trees shown in 3-11, starting from the second level, represent a breadth-first search for the first layer of the tree.
3.6Dijkstra Single-source Shortest path search algorithm
Dijkstra algorithm is a non-heuristic function version of a start algorithm, and it is a typical greedy algorithm. The algorithm does not use the distance information of the node itself and the starting point and the destination point to guide, search in all nodes of the whole map, reflect its blindness, the efficiency is naturally lower than a * algorithm. The algorithm's essential search idea is that for a and B two nodes, if you add some intermediate nodes from the entire map to make the weighted path length between A and b shorter, the intermediate nodes are added to form a shorter weighted path between A and B, until there is no intermediate node so that the path between A and b two points is shorter.
When the algorithm initializes the direct distance table of each node to other nodes, the distance between nodes is the weight value of the node connection, and the unconnected distance is set to infinity. Constructs an S table that represents a collection of completed nodes, where the algorithm starts with the S table, then searches for the nearest point from the start to the S table, and takes the newly added node as the intermediate node, calculates the relative closer distance from the starting point to other nodes, and updates the distance table of the starting point until all nodes are joined to the S table. The distance value in the final distance table from the starting point is the shortest path distance from the starting point to the other nodes. When the end point is determined, the records are added to the intermediate nodes between the starting points during the algorithm's operation to find the shortest route.
Assuming that the starting point in figure 3-10 of the graph is V1, the distance table initialized by the Dijkstra algorithm is shown in table 3-1:
The execution of the algorithm can be shown in table 3-2 below, a total of six nodes to perform five cycles to add all nodes to the S table, W for the new join node, D[vi] represents the shortest distance from the starting point to the node Vi.
3.7 Random path finding algorithm
Monsters in the game have their own natural random movements when the player is not near, moving in a random direction and at random rates, making it appear to be active. When the player is close to the "realm" of the monster, the mob will actively seek to approach the player, and when the player and the monster are close enough to enter the monster's "attack area", the mob starts attacking the player.
The realization of random pathfinding algorithm is to design a random moving rule, which can make the game characters constantly produce irregular and unpredictable actions, so that the characters stay active in the game and improve the realism of the game. The stochastic of the pathfinding is mainly the direction of character marching and the speed of travel. The representation of a direction can be represented by a random destination location, or by a vector of unit directions. The change in speed can be achieved by controlling the frequency of updates or the distance of each move.
For example, you can use the direction of a vector to represent the current direction of travel, the size of the modulus to represent the speed of travel, according to a certain period of random update direction and the size of the module to achieve random movement, while using collision avoidance algorithm to correct the path, to prevent the game role in the obstacles in the time lag of the phenomenon.
Summary and improvement of AI algorithm in game