I'm a bit puzzled by the behavior of a piece of my code:
The following code creates two sprites. One should constantly turn to face the mouse cursor, the other one moves at the first sprite.
For this I call AngleBetweenVectors. The second sprite moves into the right direction, which shows that AngleBetweenVectors returns the correct result.
But the first sprite (which is supposed to face the cursor) turns into the wrong direction. (It doesn't face into the wrong direction, but turns wrongly.)
As far as I can see, however, my code should be correct this far.
But since I haven't found anyone else complaining about that there must be something wrong ~
You can copy pasta that directly into MSVC and it'll work, if anyone wants to try.
#include <SFML/Graphics.hpp>
// (180 / PI = 57.3065)
float AngleBetweenVectors(sf::Vector2f a, sf::Vector2f b){
return 57.3065f * atan2(b.y - a.y, b.x - a.x);
}
void main () {
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "");
App.SetFramerateLimit(50);
sf::Image img(50, 10, sf::Color(150,150,150,255));
sf::Sprite a(img);
sf::Sprite b(img);
a.SetPosition(200,200);
b.SetPosition(400,500);
a.SetCenter(25,5);
b.SetCenter(25,5);
float travel=AngleBetweenVectors(b.GetPosition(), a.GetPosition());
while(true){
sf::Event event;
App.GetEvent( event);
sf::Vector2f mousePos(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
float f=AngleBetweenVectors(a.GetPosition(), mousePos);
// x = speed * cosine( angle * PI/180 )
// y = speed * sine( angle * PI/180 )
float nx= .25 * cos(travel * 0.01745);
float ny= .25 * sin(travel * 0.01745);
b.Move(nx,ny); //Make b move towards a
a.SetRotation(f); //Make a face the cursor, doesn't work.
//a.SetRotation(360-f); //Make a face the cursor, does work.
App.Clear();
App.Draw(a);
App.Draw(b);
App.Display();
}
}