将Sprite旋转到鼠标位置 [英] Rotate Sprite to Mouse Position

查看:143
本文介绍了将Sprite旋转到鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆