开发者

Rotate Sprite to Mouse Position

I've been using the SFML 1.6 library, and I'd like to know.

How d开发者_如何学Gooes 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

Rotate Sprite to Mouse Position

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

Rotate Sprite to Mouse Position

= (1, 0, 0).

To calculate the angle you can use the cross product :

Rotate Sprite to Mouse Position

And then :

Rotate Sprite to Mouse Position

then you calculate the angle :

Rotate Sprite to Mouse Position

Finally You rotate your sprite :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜