Back to Home

Steering behavior. Types of changing the character’s direction of movement

ActionScript · steering behavior · game programming

Steering behavior. Types of changing the character’s direction of movement

When developing games, it is often necessary to implement following a certain route. For example, the character needs to pave the way from point A to point B. Suppose we calculated it using some algorithm for finding the path, let's go. And then it turns out that another unit comes from point C to point D and crosses the road for us and we need to get around it. What to do? Constantly rebuilding the path is unprofitable, there are a lot of extra computational costs when it is enough to slightly change the direction already during movement to avoid a collision.
Types of direction changes along the way are steering behaviors.

This article is a translation of the first article from the Understanding Steering Behaviors series written by Fernando Bevilacqua.

It turned out to be difficult to find an adequate translation in the Russian language. I decided to use the term “steering”, which I met most often on the Internet. Stiring helps characters move in a realistic manner, using simple forces, the combination of which allows for a very natural movement of characters in the world around them. The ideas behind stirating were proposed by Craig Reynolds . They do not use complex strategies related to path planning or huge calculations; instead, they use available information, for example, 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, you should have a general understanding of the mathematics of vectors. For those who want to refresh their knowledge, I recommend that you read the next article ( Linear Algebra for Game Developers ).

Position, Speed ​​and Movement


To realize all the forces involved in changing direction in the direction of movement can be using mathematical vectors. Since these forces will affect the speed and position of the character, using 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 - his coordinates (x, y); V (a, b) is the velocity vector. The movement is calculated using the Euler method .

position = position + velocity

The direction of the speed vector will control where the character is heading, while the length of the vector shows how far the character will move 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 by some value; as a rule, the maximum speed in the simulated world is used.
Speed ​​is calculated as follows:

velocity = normalize(target - position) * max_velocity,

where target is the goal we are moving towards, position is the character’s current position, max_velocity is the maximum speed.

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

Force calculation


One of the ideas of stiring is to influence the character’s movement by adding steering forces. Depending on them, the character will move in one direction or another.

For seek bahavior behavior, adding control power to the character makes him smoothly adjust his speed, avoiding sudden changes in the route. If the target moves, the character will gradually change its velocity vector, trying to reach the target in its new location.

Behavior The aspiration uses two forces: the desired speed (desired velosity) and control 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 in the direction of 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 vector of control force is calculated, it must be added to the character (to the velocity vector). Adding control power to each moment in time will allow the character to abandon the direct route and head towards the target in a smooth line (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 cannot exceed the maximum allowed force that can act on the character. Also, the control 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


Flee behavior uses the same powers 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 character’s position from the target’s position. The result is a vector that goes from the target to the character. The control force is calculated almost in the same way:

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 escape from the target. The control force allows the character to abandon the current route, pushing him in the direction of the desired velocity vector.
Thus, the following correspondence can be established between the resulting vectors in the behavior of Aspiration and Avoidance:

flee_desired_velocity = -seek_desired_velocity

In other words, the vectors are opposite to each other in direction.

After the control force vector is calculated, it must be added to the character’s speed 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 (orange curve in the figure below)


Conclusion


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

Read Next