# Steering Behaviors

Well, what are the Steering Behaviors ?
They are collection of techniques that helps the autonomous characters move in realistic manner, by using some forces that are combined together to produce a normal and natural movement.
This technique contains many behaviors, but i will explain the following:-

• Seek.
• Flee And Arrival.
• Wandering.

The direction of the velocity controls where the plane is heading or going to while its length controls how much it will move every frame, so, the greater of the length, the faster the plane moves.
To measure the wanted velocity vector that help the plane to reach to its target calculated as follows

```Vector3  Normal = Vector3.Normalize(GameObject.Position - TargetPosition);
Vector3 DesiredVelocity = Normal * MaxVelocity;
```

This for only for calculating the velocity that the Enemy plane should use to reach the player, but without the steering the Plane will go in “Straight” Routes, which won’t be realistic, so we should add a “Steering force” that  makes the plane smoothly adjust its velocity, avoiding sudden path change , when the player plane changes its direction, the enemy “Gradually” changes its velocity vector to reach the player’s new position. Those forces calculated as follows :-

```Vector3  Normal = Vector3.Normalize(TargetPosition - GameObject.Position);
Vector3 DesiredVelocity = Normal * MaxVelocity;
return (DesiredVelocity - GameObject.LinearVelocity);
```

After adding all these forces, there will be a path that leads the enemy to the player positions every time.

Flee

This behavior uses the same forces that help the enemy plane seeking the Player, but it differs that it helps the enemy to Run Away from the player. The desired velocity that helps the enemy to run away is calculated by subtracting the enemy’s position from the player’s position which produces a New vector that goes from the Player’s vector  towards the enemy.

```Vector3  Normal = Vector3.Normalize(GameObject.Position - TargetPosition);
Vector3 DesiredVelocity = Normal * MaxVelocity;
return (DesiredVelocity - GameObject.LinearVelocity);
```

It is obvious that Flee Steering  =  -Seek Steering.

Now we should add these forces to create a “Fleeing Path” … Arrival

This technique prevents the enemy from moving through the player, by slowing down its velocity when it reaches a specific area until it stops at the player .

This behavior consists of 2 phases or cases,

• ⦁ When the enemy is outside the player area
• ⦁ When it is in that area

The First phase is like the “Seeking behavior”, move to the player position with a capable velocity, the second phase when it reaches the player’s area. When the enemy enters the slowing area its velocity is decreased linearly to ZERO, and this achieved by adding “Arrival Steering” , this addition reduces the enemy’s velocity till it stops.
but we should make sure that the enemy’s velocity will turn ZERO immediately , it should be decreased gradually.
to calculate the desired velocity and the distance to player :-

```Vector3 DesiredVelocity  = TargetPosition - GameObject.Position;
float Distance = DesiredVelocity.Length();
```

Then we should check if the Enemy is in far from the Player or not(Distance to target  > 0), else we will return a new vector3()  :-

```if(Distance > 0)
{
float Speed = (Distance / DeclerationVelocity * .3f);
Speed = TruncateFloat(Speed, MaxVelocity);
Vector3 DesiredVelocity = DistanceToTarget * Speed / Distance;
return (DesiredVelocity - GameObject.LinearVelocity);
}
else
{
return new Vector3();
}
```

P.S: the enemy’s original vector doesn’t change , but the addition vector affects to it that turns it to NULL, but when it is outside the Slowing area it will use its original velocity.

Wandering

The wandering steering help the enemy to act a realistic movement , which will make the player know that there is an enemy around and should take care about it.

The idea of implementing the wandering behaviors is producing a small random and apply to enemy’s current direction vector, these small displacement prevents the enemy’s plane to change its direction and rout immediately by changing its angle in every frame when the plane go right or left.

Now, we can use a circle in front of the enemy’s plane like that :- The displacement force will interfere with the enemy’s plane route to calculate the Wander Force .
First of all we should determine the position of the Circle which will be in front of the Plane, and that can be done by make a copy of the velocity vector which mean that the center of the circle will in the same direction of the velocity vector, then it is normalized and multiplied by a scalar value.
Second, we should determine the displacement force , we can use a 2D vector instead of 3D, because the wandering is about moving the plane Right or left , it is known that greater of the circle radius the stronger the Wandering steering.
By implementing this behavior the plane could move randomly in the space.

```WanderTarget = new Vector2(RandomClambed() * WanderJitter, RandomClambed() * WanderJitter);
WanderTarget.Normalize();
Vector2 DesiredLocalTarget = WanderTarget + new Vector2(WanderDistance, 0);
```

A pursuit is a process of following a target to catch it, but it differs from Seeking that pursuit is predicting where the target will be in the future, by implementing this technique it is possible to adjust current trajectory to avoid unnecessary paths. The pursuit is like seeking behavior, the only difference that the pursuer won’t seek the target itself, but its future position, so the implementation won’t be very different, the following figure shows what we want to do :- To know the future position of the target , we will use “Euler Integration” :

```Position = Position + Velocity;
```

But we want to know the position in the future, so we will give the equation a factor of Number Game Updates (N), So this is the final equation :

```Position = Position + Velocity * N;
```

If the value of N is high, this will lead to a good prediction, if N is close to Zero this means that the pursuer enemy won’t get the player position in the future.
After that we call the Seeking technique to get the path to future position.

```return Seek(((TargetPosition + Target.LinearVelocity) * LookAheadTime));
```

LookAheadtime is a variable that has the value of Number of game updates.
the full implementation is as follow :-

```
Vector3 targetPos = aTarget.Position;
//Check if face to face;
Vector3 DistanceToVectorTarget = targetPos - GameObject.Position);
{
return Seek(aTarget.Position);
}
float Speed = Magnitude(GameObject.LinearVelocity);
//Predict the Direction to the target and head to it
float LookAheadTime = (DistanceToVectorTarget.Length() / (aTarget.SteeringBehaviors.MaxVelocityFunction + Speed));

return Seek(((targetPos + aTarget.LinearVelocity) * LookAheadTime));

```

The opposite of the pursuit behavior , the enemy should flee away of the future position of the player. To implement this behavior, we will do the same as Pursuit behavior, but this time we will call the Flee Function :-
The implementation is as follow :-

```
Vector3 targetPos = aTarget.Position;
//Check if face to face;
Vector3 DistanceToVectorTarget = targetPos - GameObject.Position);
{
return Seek(aTarget.Position);
}
float Speed = Magnitude(GameObject.LinearVelocity);
//Predict the Direction to the target and head to it
float LookAheadTime = (DistanceToVectorTarget.Length() / (aTarget.SteeringBehaviors.MaxVelocityFunction + Speed));