我正在尝试围绕一个枢轴点(在这种情况下是原点)旋转刚体,而不是其质心。
有人建议应用三个变换:
将刚体变换到原点
以其质心为中心旋转刚体
将刚体从原点移开。
以下是我的代码:
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());
btVector3(0.0, 2.0, 0.0)
,那么这两个代码片段的作用完全相同。如果它被设置为btVector3(-2.0, 0.0, 0.0)
,显然不会起作用,因为您尝试围绕它所在的轴旋转点,这当然会使其停留在原地。 - Estiny