绕轴心点旋转刚体

Rotating a RigidBody around a pivot point

我正在尝试围绕枢轴点(在本例中为原点)而不是其质心旋转刚体。

我建议应用三个转换:

  1. 将刚体变换到原点

  2. 在其质心上旋转刚体

  3. 从原点变换刚体。

这是我的代码:

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());

这个方法确实管用,只是你用错了。您的第二次平移是沿世界轴执行的,但您已经旋转了对象,因此您必须将其沿旋转矢量平移回来。

正确的代码应该大致如下所示:

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);  

//Get rotation matrix
btTransform invRot(btQuaternion(btVector3(1, 0, 0), btScalar(degreesToRads(-1))),btVector3(0,0,0));
//Rotate your first translation vector with the matrix
btVector3 invTrans(-2.0 * axis.getX(), 0.0, -2.0 * axis.getZ());
invTrans = invRot * invTrans;

//Update axis variable to apply transform on
orn.getRotation(quat);
axis = quat.getAxis();

//Translate back by rotated vector
btPhys->translate(-invTrans); 

我不确定旋转是否不应该是减号(我现在无法检查)但是你可以轻松地尝试两者。

编辑。

好的,所以你忘了说你执行的是连续旋转而不是单个旋转。此过程对于围绕枢轴的单次旋转(例如 30 度旋转)是正确的。我再次查看了您的代码,我了解到您尝试沿本地 x 轴和 z 轴执行第一次翻译。然而,事实并非如此。在这一行中:

btVector3 axis = quat.getAxis();

变量 axis 是一个单位向量,表示对象围绕其旋转的轴。它不是它的坐标系。我以前没有注意到这部分。四元数很棘手,你应该阅读更多关于它们的信息,因为很多人都在误用它们。

在连续情况下可行的解决方案是在您的对象中存储最后一次平移(从质心到枢轴 - 在我的示例中它由 invTrans 表示)并使用它来执行第一次平移,然后照原样旋转,用它移动到合适的位置。

更正后的代码如下所示:

btMatrix3x3 orn = btPhys->getWorldTransform().getBasis();   
btQuaternion quat;
orn.getRotation(quat);

//Move rigidbody 2 units along its axis to the origin
btPhys->translate(btPhys->offsetToPivot);

//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);  

//Get rotation matrix
btTransform invRot(btQuaternion(btVector3(1, 0, 0), btScalar(degreesToRads(-1))),btVector3(0,0,0));
//Rotate your first translation vector with the matrix
btVector3 invTrans = invRot * btPhys->offsetToPivot;

//Update axis variable to apply transform on
orn.getRotation(quat);
axis = quat.getAxis();

//Translate back by rotated vector
btPhys->translate(-invTrans); 
btPhys->offsetToPivot = invTrans;

但是,在开始整个过程​​之前,您必须将 offsetToPivot 设置到相对于质心的位置。

我的印象是你的问题的主要原因是缺乏对线性代数和基本空间变换的理解。如果您打算继续从事该领域,我强烈建议您阅读本主题。把你的问题画在纸上也很有帮助。

编辑2.

好的,我试过你的代码:

btVector3 temp = vec3(0,2,0);
btTransform invRot(btQuaternion(btVector3(1, 0, 0), btScalar(-0.017453f)),btVector3(0,0,0));
temp = invRot * temp;

此后,temp等于{0.000000000, 1.99969542, -0.0349042267}

在下面的函数中,这些转换执行您描述的三个步骤:

int x = cos(angRads) * (initial.x - axisOfRotation.x) - sin(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.x;      
int y = sin(angRads) * (initial.x - axisOfRotation.x) + cos(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.y;

即:

Step 1:Transform the rigidbody to the origin.

    initial.x - axisOfRotation.x
    initial.y - axisOfRotation.y

Step 2:Rotate the rigidbody on its center of mass.

    cos(angRads) * initial.x - sin(angRads) * initial.y
    sin(angRads) * initial.x + cos(angRads) * initial.y 

Step 3:Transform the rigidbody off of the origin.

    +axisOfRotation.x;
    +axisOfRotation.y;

这是一个递归函数,可以完全满足您的需要 returns 向量中所有连续旋转的点:(以它为基准)

rotateCoordinate(vector<Point>& rotated, Point& axisOfRotation, Point initial, 
                            float angRads, int numberOfRotations){
    // base case: when all rotations performed return vector holding the rotated points
    if(numberOfRotations <= 0) return;
    else{
        // apply transformation on the initial point
        int x = cos(angRads) * (initial.x - axisOfRotation.x) - sin(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.x;
        int y = sin(angRads) * (initial.x - axisOfRotation.x) + cos(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.y;
        // save the result
        rotated.push_back(Point(x, y));
        // call the same function this time on the rotated point and decremented number of rotations
        rotateCoordinate(rotated, axisOfRotation, Point(x,y), angRads, numberOfRotations -1);
    }
}

其中 Point 是:

struct Point {
    int x, y;
    Point(int xx, int yy) : x(xx), y(yy) { }
    Point() :x(0), y(0) { }
};

进一步阅读解释其背后的数学原理 here