Steering Algorithm Problem
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
if (alpha > PI)
if (beta > (alpha - PI))
if (beta < (alpha + PI))
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.