我想學習三維程序設計,現在我試圖瞭解如何使用四元數來圍繞一個軸旋轉矢量。用四元數圍繞一個軸旋轉矢量
據我所知,要將一個向量v圍繞軸a旋轉,將兩個向量轉換爲四元后,我們將v乘以a,然後乘以a的共軛。 (0,0,1)(或0,0,0,0,0)(0,0,0)(0,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
R值不要歸一化: X = 0.707107,Y = 0,Z = 1,W = -2.12132 ř標準化: X = 0.288675,Y = 0,Z = 0.408248,W = -0.866025
我究竟做錯了什麼? 我甚至能理解這個過程中的任何事情嗎?
你可以使用任何現有的四元數實現庫(旋轉)來研究和驗證自己的數學 – minorlogic