我试图围绕枢轴点(在本例中是原点)旋转刚体,而不是其质心。围绕枢轴点旋转刚体
我有一个建议,应用三个转变:
变换的刚体原点
转动它的质量
的中心刚体变换刚体掉的起源。
这里是我的代码:
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