# Steering Algorithm Problem

This is a discussion on Steering Algorithm Problem within the Game Programming forums, part of the General Programming Boards category; Ok, So I've been working on this 2D game for a while, actualy a bunch - some I left unfinished ...

1. ## Steering Algorithm Problem

Ok,
So I've been working on this 2D game for a while, actualy a bunch - some I left unfinished for some other ideas and some I've finished... but I seem to always run into problems with the same thing... choosing which way to turn the entity to achieve a given "course". My algorithm will always make the entity's direction "jump" sometimes instead of gradually turning, why do you think this is?

This are the problem's parameters:
- An Entity can turn at a certain rate, either left or right.
- A chosen direction is given for the entity to face.
- The entity must then choose the shortest way to start turning towards that direction... left or right.
- Entity's angle and desired angle are represented in radians, by doubles from 0 to 2*PI. No angle can go over 2*PI or under 0, if they do, they are subtracted 2*PI or added 2*PI respectively...

Method I've been using:
alpha - initial direction
beta - desired direction
Code:
```if (alpha > PI)
{
if (beta > (alpha - PI))
left
else
right
}
else
{
if (beta < (alpha + PI))
right
else
left
}```
====
Then I take the direction found and if it's left, I subtract the turning speed angle from current angle or if it's right I add to it.
BUT - before adding/subtracting I check to see that when we add/sub the turn speed, we don't pass the desired angle, if we will, I don't add/sub I just set the current direction to the desired one.

2. In my experience, the best way to do this is to normalize to one of your angles instead of zero, that way you avoid wrapping around the long way. I would say something like:
Code:
```while desiredHeading > currentHeading + PI: