2011-02-01 87 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