【发布时间】:2015-09-09 22:26:00
【问题描述】:
我正在尝试围绕枢轴点(在本例中为原点)而不是其质心旋转刚体。
我有一个建议应用三个转换:
将刚体转换为原点
围绕质心旋转刚体
从原点变换刚体。
这是我的代码:
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());
【问题讨论】:
-
您应该将枢轴点转换为原点,然后旋转,然后应用反向转换。
-
我已经编辑了我的答案。
-
对我来说,假设 offsetToPivot 最初设置为
btVector3(0.0, 2.0, 0.0),这两个代码片段的工作方式完全相同。如果它被设置为btVector3(-2.0, 0.0, 0.0);它显然不会工作,因为你试图围绕它所在的轴旋转点,这当然会把它留在同一个地方。
标签: c++ bulletphysics