2011-02-01 86 views
3

我一直在使用SFML 1.6库,我想知道。将Sprite旋转到鼠标位置

一个人如何旋转一个精灵,以便它总是转向鼠标在屏幕上的位置?

谢谢。

(SFML特定代码是优选的)

回答

8

如果具有子画面的位置:S =(SX,SY)和光标C =的位置

(CX,CY)你可以计算矢量enter image description here =(Cx-Sx,Cy-Sy)与例如enter image description here =(1,0,0)的单位矢量之间的角度。

要计算的角度,你可以使用cross product

enter image description here

然后:

enter image description here

那么你计算角度:

enter image description here

最后你旋转你的精灵:

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

上帝的圣母。更烦人的数学。 :( – Lemmons 2011-02-05 23:48:06

2

有关于在this link的SFML论坛的讨论。

+1

现在这个链接已经死了。 – 2014-11-20 22:23:20