Rotate Sprite to Mouse Position

倖福魔咒の 提交于 2019-12-23 19:16:38

问题


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)


回答1:


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

= (Cx - Sx, Cy - Sy) and a unit vector for example

= (1, 0, 0).

To calculate the angle you can use the cross product :

And then :

then you calculate the angle :

Finally You rotate your sprite :

Sprite.SetRotation(alpha); //alpha in degree


来源:https://stackoverflow.com/questions/4859306/rotate-sprite-to-mouse-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!