SFML 的 vanilla 开发包和完整的 SDK 下载都包含矢量类声明/定义文件。在 ../include/SFML/System/(在任一包中)中查找文件 Vector2.hpp 和 Vector2.inl。添加旋转功能的一种方法可能是:
将rotate方法添加到Vector2.hpp中的类声明中:
...stuff...
template <typename T>
class Vector2
{
public :
....
void Rotate(T angle);
....
};
然后在vector2.inl中定义方法(遵循约定):
template <typename T>
void Vector2<T>::Rotate(T angle) {
...your implementation here...
}
修改 Vector2 类的替代方法是使用 quite nifty, SFML-based Thor library 中的扩展向量数学函数,其中包括一个 Rotate 函数。使用 Thor SDK 的 2D 矢量函数所需的最小(未更改)文件是:
- ../include/Thor/Vectors/VectorAlgebra2d.hpp
- ../include/Thor/Detail/VectorAlgebra2D.inl
- ../include/Thor/Math/Trigonometry.hpp
- ../src/Trigonometry.cpp
使用项目目录中的这些文件,您可以通过执行以下操作来旋转 sf::Vector:
#include <iostream>
#include <SFML/Graphics.hpp>
#include "VectorAlgebra2D.hpp"
....
sf::Vector2f rotate_THIS(10.0f,10.0f);
thor::Rotate(rotate_THIS, 180.0f); //pass by reference
std::cout << "(" << rotate_THIS.x << ", " << rotate_THIS.y << ")" << std::endl;
sf::Vector2f rotated = thor::RotatedVector(rotate_THIS, 180.0f); //returns object
std::cout << "(" << rotated .x << ", " << rotated .y << ")" << std::endl;
....
哪些输出(可预测):
(-10,-10)
(10,10)
我刚刚经历了需要为项目修改 SFML 矢量类模板的相同过程,在查看了源代码并添加了一些函数(长度和点积)之后,我偶然发现了 Thor 库,它到目前为止对我的帮助很好。