Assuming you have a missile X/Y velocity, you could greatly simplify this by employing linear algebra. You could eliminate the atan2 calls altogether and substitute it with perpendicular dot product.
template <typename T>
inline T PerpDot(const sf::Vector<T>& A,const sf::Vector<T>& B)
{
return (A.x * B.y) - (A.y * B.x);
}
Given 2 vectors A and B, PerpDot(A,B) gives you sin( theta ) * length(A) * length(B). (Where 'theta' is the angle between the vectors)
For example, if A and B are parallel, PerpDot(A,B) would be zero. You can use this to determine which "side" of a vector another point is on.
You have 3 key points:
i) the missile's current position
ii) Any point along the missile's current trajectory (missile's current position + missile velocity)
iii) The target point
Vector A would be ii - i.
Vector B would be iii - i.
Run a PerpDot on those vectors. If the result is negative, rotate one way. Or if it's positive, rotate the other way.
If the result is zero, then the target point is either directly in front of (or directly behind) the missile. In which case you can do a Dot product to figure out which:
template <typename T>
inline T Dot(const sf::Vector<T>& A,const sf::Vector<T>& B)
{
return (A.x * B.x) + (A.y * B.y);
}
Dot(A,B) gives you cos(theta) * length(A) * length(B).
The result here is that Dot(A,B) will be positive if the missile is heading towards the target, and negative if the missile is heading away from it.