将Sprite旋转到鼠标位置 [英] Rotate Sprite to Mouse Position
问题描述
我一直在使用SFML 1.6库,我想知道。
如何旋转sprite,使它总是转向鼠标
$ b
如果你有sprite的位置:S =(Sx,Sy)和光标的位置C =(Cx,Cy) p>
您可以计算向量 =(Cx-Sx,Cy-Sy)和单位向量,例如 = ,0,0)。
要计算角度,您可以使用 cross产品:
然后:
然后计算角度:
最后,旋转你的精灵:
Sprite.SetRotation(alpha); // alpha in degree
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)
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
这篇关于将Sprite旋转到鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!