Rotate the sprite to mouse position

I am using the SFML 1.6 library and I would like to know.

How to rotate a sprite so that it always rotates in the direction where the mouse is on the screen?

Thank.

(preferred specific SFML code)

+3
source share
2 answers

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

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

To calculate the angle, you can use 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
+8
source

This is discussed in the SFML forum at this link .

+2

Source: https://habr.com/ru/post/1789159/


All Articles