1. Overview
Potential functions are physics principles. We mainly use potential functions to control unit behaviors in the game. For example, we can use potential functions to build groups, simulate group movement, deal with Chase and Dodge, and avoid obstacles. The potential function we specialize in is called the Lenard-Jones potential function.
In physics, Lenard-Jones potential energy represents the potential energy of attracting and rejecting molecules. Here, u represents the potential energy in the atom, Which is inversely proportional to the interval between molecules and R. A and B are parameters, which are the same as those of M and N. If we take the derivative of this potential function (derivative), we can get a function that represents a certain force. Based on the closeness of the two molecules, this force function generates gravity and exclusive force. In our case, the molecules refer to the units in the game that are moving. This kind of ability that can represent gravity and exclusive force can benefit us. By adjusting the parameters, you can convert the gravity and exclusive force to achieve Chase and Dodge. In addition to chasing and Dodge, Alibaba Cloud uses the exclusive force to avoid obstacles, and uses gravity to form groups.
Figure-Potential Function Image
2. Chase and dodge
There are two planes, craft1 and craft2. craft1 is controlled by players, and craft2 is controlled by computers. By calculating the potential energy of craft2 and craft1, obtain the force of craft2 to control the Moving Mode of craft2.
Void DoAttractCraft2 (void ){
Vector r = Craft2.vPosition-Craft1.vPosition;
Vector u = r;
U. Normalize ();
Double U, A, B, n, m, d;
A = 2000; // gravity intensity
B = 4000; // exclusive strength
N = 2; // gravitation Attenuation
M = 3; // rejection Attenuation
D = r. magn1_()/Craft2.fLength; // scale scaling is considered.
U =-A/pow (d, n) + B/pow (d, m); // The potential energy is actually obtained here, and this potential energy is used as the force size later.
Craft2.fa = vrotate2d (-craft2.forientation, u * U); // U * u forces in one direction. Then, after rotating the coordinate axis, add the force to the Unit.
Craft2.pa. x = 0; // the last few words are not understood.
Craft2.pa. Y = craft2.flength/2;
Target = craft1.vposition;
}
By adjusting parameters, some results are generated, such:
Figure-tracking and dodge of potential energy
· (A) in the process, the chaser advances toward the prey. When the prey passes away from him, he will go back. When the tracking speed is too close, it will suddenly turn around to maintain some separation distance between the two units, it feels like it is too fast to run, and then back to chase.
· In (B), when we reduce the intensity of the gravity component (reduce the value of A by A bit), the result is very similar to blocking, and it feels like it is just running and hitting.
In (C), we increase the intensity of gravity, and the results are similar to the basic line of sight algorithm. How much is the enhancement bigger than the one in? This is unknown.
· (D) in, we reduce gravity, increase rejection, and adjust the index parameters. As a result, the unit controlled by the computer will escape the players.
3. Avoid obstacles
The basic idea is to set the parameter to 0, leaving only the exclusion force component. By adjusting parameter B, the rejection strength and the exponential m are determined, and the attenuation degree is determined.
For example, there are many obstacles on the map. The computer controls a unit to move on the map and can avoid obstacles. When calculating the exclusion force, we need to take into account all the organizational unit's exclusive force. The overall result is the actual force on the organizational unit.
Below is a program running effect that can avoid obstacles:
Figure-Potential Function avoiding obstacles
4. Groups
Using Potential Energy functions to implement group behavior does not require basic grouping rules. Instead, you only need to calculate the potential energy between any two units to obtain the power of each unit.
The code is ignored. The following is a group image:
Figure-groups
(A) These units are aggregated. (B) is the path of each organization. Obviously, paths are intertwined. Such a result is like a Collective Behavior of bees or flies.
In addition, if a group can catch prey and avoid obstacles, it is better to combine the potential energy produced by the pursuit and avoidance with the potential energy of the group. Of course, parameters of potential energy functions for different purposes are different.
In addition, if you specify a specific unit as the group owner and then make it attract other units, then when the group owner moves, this group of groups tends to be self-organized and more elegant group behavior.
5. Optimization suggestions
The complexity of the group clustering algorithm using potential energy is N * N. Below we try to reduce the complexity through optimization.
For obstacle avoidance algorithms, potential energy can not be calculated for long-distance obstacles. If the distance is far away, the influence is also small, regardless of the gravity or rejection. Set a range threshold. In this way, although it is necessary to determine whether all obstacles are within the threshold, that is, it is still N, but the complexity of the calculation potential energy is much smaller.
If you want to reduce the complexity to N, you can divide the game field into networks. Each grid is configured with an array to store the Obstacle Data falling into the grid. When calculating the potential energy of a unit, we only need to process the units in the same grid as the unit and the units in the circle around the grid. In this way, it is reduced to less than N.
For the clustering algorithm, the grid can be the complexity of N * N, which is reduced to a constant multiple of N, which can greatly save the CPU time. For the potential energy between I and j, only one calculation is required.