3

I've been using the SFML 1.6 library, and I'd like to know.

How does one rotate a sprite so it always turns toward where the mouse is located on screen?

Thanks.

(SFML specific code is preferred)

Lemmons
  • 1,738
  • 4
  • 23
  • 33

1 Answers1

8

If you have the position of the sprite : S = (Sx, Sy) and the position of the cursor C = (Cx, Cy)

You can calculate the angle between the vector enter image description here = (Cx - Sx, Cy - Sy) and a unit vector for example enter image description here = (1, 0, 0).

To calculate the angle you can use the cross product :

enter image description here

And then :

enter image description here

then you calculate the angle :

enter image description here

Finally You rotate your sprite :

Sprite.SetRotation(alpha); //alpha in degree
Ghassen Hamrouni
  • 3,138
  • 2
  • 20
  • 31