# Steering Algorithm Problem

• 10-17-2006
Warlax
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.
• 10-17-2006
ichijoji
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:     desiredHeading -= 2 * PI while desiredHeading <= currentHeading - PI:     desiredHeading += 2 * PI diffHeading = desiredHeading - currentHeading currentHeading += diffHeading * turnSpeed * dtime```
Where turnSpeed is some value between 0 and 1 that's basically the gain on your feedback, telling the system how much of the error in heading to correct every second.
• 10-17-2006
Warlax
wow,
I dont think I quite understand what you're doing here...
could you explain this a little more? how is this going to solve my "jumping around" problem?