Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rotate Sprite  (Read 3216 times)

0 Members and 1 Guest are viewing this topic.

prondzyk

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rotate Sprite
« on: July 01, 2009, 10:17:33 pm »
Hi my first post :)
I was writning code which rotate sprite and follows the cursor.
Code: [Select]

Sprite.SetCenter(Sprite.GetSize() / 2.f);
if (App.GetInput().GetMouseX() <= Sprite.GetPosition().x) {
   Sprite.SetRotation  (-1 * 360/ 3.1415926535 * (atan(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) / static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))));
      } else {
      Sprite.SetRotation  (-1 * 360/ 3.1415926535 *(atan(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) /      static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))));
      }

but it doesn't work properly. Sprite (its rifle)isn't parallel to cursor.
It look like that:


What is wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotate Sprite
« Reply #1 on: July 01, 2009, 11:15:17 pm »
First, use atan2 rather than atan. The former gives the unique angle corresponding to the cos/sin pair, while the latter will give the same result for opposite angles (cos/sin == -cos/-sin).

Then, you should use App.ConvertCoords to map mouse coordinates to "scene" coordinates. It may not be important if you use the default view, but it's cleaner and will avoid stupid errors if you use views later ;)
Laurent Gomila - SFML developer

prondzyk

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rotate Sprite
« Reply #2 on: July 02, 2009, 10:01:27 am »
Now I have:
Code: [Select]

Sprite.SetCenter(Sprite.GetSize() / 2.f);
if (App.GetInput().GetMouseX() <= Sprite.GetPosition().x) {
   Sprite.SetRotation  (-1* 360 / 3.1415926535 * (atan2(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) , static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))));
      } else {
      Sprite.SetRotation  (-1* 360 / 3.1415926535 *(atan2(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) ,      static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))));
      }
App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());

but still doesn't work properly. The same story :/

EDIT:
Success! It must look like this:
Code: [Select]
Sprite.SetCenter(Sprite.GetSize() / 2.f);
if (App.GetInput().GetMouseX() <= Sprite.GetPosition().x) {
   Sprite.SetRotation  (((-1* 360 / 3.1415926535 * (atan2(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) , static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))))/2)+90);
      } else {
      Sprite.SetRotation  (((-1* 360 / 3.1415926535 *(atan2(   static_cast<double> (Sprite.GetPosition().y - App.GetInput().GetMouseY()) ,      static_cast<double> (Sprite.GetPosition().x - App.GetInput().GetMouseX()))))/2)+90);
      }

Thanks for help!

 

anything