c++ - 将 Sprite 旋转到鼠标位置

标签 c++ sfml

我一直在使用 SFML 1.6 库,我想知道。

如何旋转 Sprite 使其始终转向屏幕上鼠标所在的位置?

谢谢。

(首选 SFML 特定代码)

最佳答案

如果你有 Sprite 的位置:S = (Sx, Sy) 和光标的位置 C = (Cx, Cy)

您可以计算 vector enter image description here 之间的角度= (Cx - Sx, Cy - Sy) 和一个单位 vector ,例如 enter image description here = (1, 0, 0).

要计算角度,您可以使用 cross product :

enter image description here

然后:

enter image description here

然后你计算角度:

enter image description here

最后你旋转你的 Sprite :

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

关于c++ - 将 Sprite 旋转到鼠标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4859306/

相关文章:

c++ - 在 C++ 中读取原始字节数据

c++ - 如何将 hdf5 添加到 Qt 项目中?

c++ - zlib 在 Windows 7 (x64) 上编译

qt - 在 Ubuntu 13.04 和 QT-Creator 上设置 SFML

c++ - SFML 纹理变换(放大)

c++ - VS2012 调试中 SFML 变量周围的损坏堆栈

c++ - OpenGL 是非常特定于机器的

c++ - 我怎样才能在我的窗口顶部获得空间?

c++ - opencv make error : limits. h 没有这样的目录文件

C++ - unordered_map 结构内存问题