📜 ⬆️ ⬇️

Steering behavior. Types of changing the direction of movement of the character on the go

When developing games, it is often necessary to implement the following of some route. For example, a character needs to build a path from point A to point B. Suppose we calculated it using some kind of path finding algorithm, let's go. And here it turns out that from point C to point D there goes another unit and crosses our way and we should get around it. What to do? Constantly rebuilding the path is expensive, a lot of unnecessary computational costs, when it is enough to slightly change the direction already during the movement to avoid a collision.
Types of changing direction in the course of movement and steering steering.

This note is a translation of the first article in the Understanding Steering Behaviors cycle written by Fernando Bevilacqua.

In Russian, it turned out to be difficult to choose an adequate translation. I decided to use the term "Stiring", which I met most often in Runet. Stiring helps the characters to move in a realistic manner, using simple forces, the union of which allows for very natural movement of the characters around the world around them. The ideas behind the styring were proposed by Craig Reynolds . They do not use complex strategies associated with planning paths or huge calculations, instead using available information, such as the forces acting on neighboring characters (units). This makes them easy to understand and implement, while allowing very complex patterns of movement.
To understand this article should have a general idea of ​​the mathematics of vectors. For those who want to refresh their knowledge in memory, I recommend reading the following article ( Linear algebra for game developers ).

Position, Speed ​​and Movement


You can implement all the forces involved in changing the direction of travel with the help of mathematical vectors. Since these forces will influence the character’s speed and position, the use of vectors is a good approach.
')
Although the vector should have a direction, it will be ignored when we talk about the character's position vector (we will assume that the radius vector is directed to the character's current location).


The figure above shows the character - its coordinates (x, y); V (a, b) is the velocity vector. Movement is calculated according to the Euler method .

position = position + velocity 

The direction of the velocity vector will control where the character is going, while the length of the vector indicates how far the character moves per unit of time. The greater the length of the vector, the faster the character will move. As a rule, the velocity vector can be limited to any value, as a rule, the maximum speed in the simulated world is used.
The speed is calculated as follows:

 velocity = normalize(target - position) * max_velocity, 

where target is the target to which we are moving, position is the current position of the character, max_velocity is the maximum speed

It should be noted that without styling, the character moves only in a straight line and, if the target changes, changes its direction instantly. These movements look very unnatural.

Calculation of forces


One of the ideas of the styling is to influence the movement of the character, through the addition of steering forces. Depending on them, the character will move in one direction or another.

For behavior, the desire (seek bahavior) adding control force to the character, causes him to smoothly adjust his speed, avoiding abrupt changes in the route. If the target moves, the character will gradually change its speed vector, trying to reach the target in its new location.

Behavior Aspiration uses two forces: the desired speed (desired velosity) and the steering force (steering force):



The desired speed is the force that directs the character to his goal along the shortest possible path. The control force is the result of subtracting the current speed from the desired one and pushes the character towards the target. These forces are calculated as follows (remember that all operations are performed on vectors):

 desired_velocity = normalize(target - position) * max_velocity steering = desired_velocity – velocity 

Adding strength


After the control force vector is calculated, it must be added to the character (to the velocity vector). Adding a control force every moment in time will allow the character to abandon the direct route and head for the goal along a smooth line (the orange curve in the figure below).



The addition of these forces and the final calculation of speed and location are as follows:

 steering = truncate (steering, max_force) steering = steering / mass velocity = truncate (velocity + steering , max_speed) position = position + velocity 

The control force may not exceed the maximum allowed force that can act on the character. Also, the controlling force must be divided by the mass of the character, which will allow you to calculate different speeds depending on the weight of the character.

Run away


Behavior Avoiding (flee behavior) uses the same forces as Seek Behavior, but they allow the character to move away from the target.



The new vector of the desired speed is calculated by subtracting the position of the character from the position of the target. The result is a vector that goes from the target to the character. The control force is calculated almost similarly:

 desired_velocity = normalize(position - target) * max_velocity steering = desired_velocity – velocity 

The desired speed in this case is the simplest route that the character can use to get away from the target. The control force allows the character to abandon the current route, pushing it in the direction of the desired velocity vector.
Thus, between the final vectors in the behavior of Aspiration and Avoidance, the following correspondence can be established:

 flee_desired_velocity = -seek_desired_velocity 

In other words, the vectors are opposite in direction.

Once the control force vector has been calculated, it must be added to the character’s velocity vector. Thus, this force pushes the character away from the target, and at each moment of time the character stops moving toward the target and starts moving away from it (the orange curve in the figure below)


Conclusion


Stiring is a good tool for creating realistic motion patterns. Despite the fact that the calculation is simple to implement, the method shows very good results in practice.

Source: https://habr.com/ru/post/237621/


All Articles