Hey there, I'm a bit baffled about this too.
Not about the way of doing this by itself, but I stumbled across a bug which I can't figure out (hijacking this a little bit, but since it's the same topic..)
Basically what I'm doing this this:
// 180 / PI = 57.3065
float Utility::AngleBetweenVectors(sf::Vector2f a, sf::Vector2f b){
return 57.3065f * atan2(b.y - a.y, b.x - a.x);
}
//Simply a wrapper
void Entity::SetRotation(float facing){
sprite.SetRotation(facing);
}
main () {
Entity z, p;
z.SetPosition(...);
p.SetPosition(...);
//z and p are two objects of class Entity which contain a sprite.
//p is the player and z is another sprite which is supposed to follow p around.
float f=Utility::AngleBetweenVectors(z.GetPosition(), p.GetPosition());
// x = speed * cosine( angle * PI/180 )
// y = speed * sine( angle * PI/180 )
float nx= 1 * cos(f * 0.01745);
float ny= 1 * sin(f * 0.01745);
z.Move(nx, ny);
z.SetRotation(f);
}
The code *should* work well. And determining nx and ny does work correctly. But the sprite of z is turning into the wrong direction.
To get the right rotation I need to use:
z.SetRotation(360 - f);
So AngleBetweenVectors returns a degree measured counter-clockwise and SetRotation treats it as being clock-wise.
Is that an error on my part? Or a problem with atan2?