我正在尝试学习3D编程,现在我正在尝试了解如何使用四元数来围绕轴旋转矢量。
据我所知,为了将矢量v绕轴a旋转,在将两个矢量转换为四元数之后,我们将v乘以a,然后将乘积乘以a的共轭。
我想围绕(1,0,0)旋转v(0,1,0)90度,我应该得到一个结果向量v(0,0,1)(或0,0, - 1,取决于旋转方向。)
我没有得到我期待的输出。 这是代码:
int main()
{
//I want to rotate this vector about the x axis by PI/2 radians:
Quaternion v(0, 1, 0, 0);
v.normalize();
float angle = PI / 2.0f;
float cos = math::cos(angle / 2.0f);
float sin = math::sin(angle / 2.0f);
Quaternion q(1.0f*sin, 0.0f*sin, 0.0f*sin, cos);
std::cout << "q not normalized = " <<"\t"<< q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
q.normalize();
std::cout << "q normalized = " <<"\t\t"<< q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
std::cout << std::endl;
Quaternion r;
//I multiply the vector v by the quaternion v, then I multiply by the conjugate.
r = q * v;
//do I need to normalize here?
r = r * q.conjugate();
//and here?
//shouldn't the resulting vector be 0,0,1?
std::cout << "r not normalized = " << "\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
r.normalize();
std::cout << "r normalized = " << "\t\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
std::cout << std::endl;
system("pause");
return 0;
}
这是输出:
q未标准化,与q标准化相同: x = 0.707107,y = 0,z = 0,w = 0.707107
未正常化: x = 0.707107,y = 0,z = 1,w = -2.12132 r标准化: x = 0.288675,y = 0,z = 0.408248,w = -0.866025
我做错了什么? 我是否从这个过程中了解了什么?答案 0 :(得分:-1)
基本上沿着x轴(1,0,0)以90度角旋转矢量,使用下面的方法,这适用于Euler和四元数
| 1 0 0 | | 0 | | 0 |
| 0 cos90 -sin90 | * | 1 | = | 0 |
| 0 sin90 cos90 | | 0 | | 1 |