我試圖圍繞樞軸點(在本例中是原點)旋轉剛體,而不是其質心。圍繞樞軸點旋轉剛體
我有一個建議,應用三個轉變:
變換的剛體原點
轉動它的質量
的中心剛體變換剛體掉的起源。
這裏是我的代碼:
btMatrix3x3 orn = btPhys->getWorldTransform().getBasis();
btQuaternion quat;
orn.getRotation(quat);
btVector3 axis = quat.getAxis();
//Move rigidbody 2 units along its axis to the origin
btPhys->translate(btVector3(-2.0 * axis.getX(), 0.0, -2.0 * axis.getZ()));
//Rotate the rigidbody by 1 degree on its center of mass
orn *= btMatrix3x3(btQuaternion(btVector3(1, 0, 0), btScalar(degreesToRads(-1))));
btPhys->getWorldTransform().setBasis(orn);
//Update axis variable to apply transform on
orn.getRotation(quat);
axis = quat.getAxis();
//Move the rigidbody 2 units along new axis
btPhys->translate(btVector3(2.0 * axis.getX(), 0.0, 2.0 * axis.getZ()));
然而,樞軸點反而呈現到處移動停留在一個地方(起源)的。是否有更好的方法(實際上是有效的)圍繞樞軸點旋轉剛體?
編輯: 我加了一些理智,校驗碼爲旋轉功能:
//Code that doesn't work
btVector3 invTrans = btPhys->offsetToPivot.rotate(btVector3(1.0, 0.0, 0.0), btScalar(degreesToRads(-1)));
//Values printed out are identical to offsetToPivot
printf("invTrans: %f %f %f\n", invTrans.getX(), invTrans.getY(), invTrans.getZ());
//Sanity code that DOES work
//Arbitrary vector
btVector3 temp = btVector3(0.0, 2.0, 0.0);
temp = temp.rotate(btVector3(1.0, 0.0, 0.0), btScalar(degreesToRads(-1)));
printf("temp %f %f %f\n", temp.getX(), temp.getY(), temp.getZ());
您應該將支點轉換爲原點,然後旋轉,然後應用反轉換。 – paddy
我已經編輯了我的答案。 – Estiny
對於我來說,假設offsetToPivot最初設置爲'btVector3(0.0,2.0,0.0)',那麼這兩個代碼片段的工作原理完全相同。如果它被設置爲'btVector3(-2.0,0.0,0.0);'它顯然不會工作,因爲你試圖圍繞它所在的軸旋轉點,這當然會將它放在同一個地方。 – Estiny