C++ 使用四元数绕轴旋转向量
我正在学习3d编程,现在我正在学习如何使用四元数绕轴旋转矢量 据我所知,要绕轴a旋转向量v,在将两个向量转换成四元数后,我们将v乘以a,然后将乘积乘以a的共轭 我想把v(0,1,0)绕a(1,0,0)旋转90度,得到一个向量v(0,0,1)(或0,0,-1,取决于旋转方向) 我没有得到我期望的结果。 代码如下:C++ 使用四元数绕轴旋转向量,c++,vector,rotation,quaternions,C++,Vector,Rotation,Quaternions,我正在学习3d编程,现在我正在学习如何使用四元数绕轴旋转矢量 据我所知,要绕轴a旋转向量v,在将两个向量转换成四元数后,我们将v乘以a,然后将乘积乘以a的共轭 我想把v(0,1,0)绕a(1,0,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:
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;
}
intmain()
{
//我想将该向量绕x轴旋转PI/2弧度:
四元数v(0,1,0,0);
v、 规范化();
浮动角度=PI/2.0f;
浮动cos=数学::cos(角度/2.0f);
浮动sin=数学::sin(角度/2.0f);
四元数q(1.0f*sin,0.0f*sin,0.0f*sin,cos);
std::cout基本上,要沿x轴(1,0,0)以90度角旋转向量,请使用以下方法,这对Euler和四元数都有效
| 1 0 0 | | 0 | | 0 |
| 0 cos90 -sin90 | * | 1 | = | 0 |
| 0 sin90 cos90 | | 0 | | 1 |
阅读旋转矩阵实际上,当我将四元数转换为旋转矩阵时,我得到了这个矩阵,但我试图理解q.v.q-1乘法是如何工作的。我的目的是“理解它是如何工作的”目的。我仍然想知道我不理解的部分:)。此外,我不是专家,但在我看来,矩阵转换+乘法是一个昂贵的过程。再次抱歉,如果我说无知的东西,只是想在这里学习:)你可以使用任何实现四元数的现有库(用于旋转)学习和验证你自己的数学