2015-09-09 132 views
3

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

我有一个建议,应用三个转变:

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

您应该将支点转换为原点,然后旋转,然后应用反转换。 – paddy

+0

我已经编辑了我的答案。 – Estiny

+0

对于我来说,假设offsetToPivot最初设置为'btVector3(0.0,2.0,0.0)',那么这两个代码片段的工作原理完全相同。如果它被设置为'btVector3(-2.0,0.0,0.0);'它显然不会工作,因为你试图围绕它所在的轴旋转点,这当然会将它放在同一个地方。 – Estiny

回答

1

这种方法的实际工作,你只是将它正确。您的第二个翻译是沿着世界坐标轴执行的,但是您已经旋转了该对象,因此您必须沿旋转的矢量将其翻译回来。

正确的代码看起来应该或多或少是这样的:

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

变量轴是一个单位向量,表示对象旋转的轴。它不是它的坐标系。我之前没有注意到这部分。四元数是棘手的,你应该多读些关于它们的文章,因为很多人都会误用它们。

解决方案将在连续的情况下工作,将对象中的最后一个翻译(从质心到枢轴 - 在我的示例中由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成相对于质量的中心地位这整个过程之前。

我有一个印象,你的问题的主要来源是缺乏对线性代数和基本空间变换的理解。如果你打算继续这个领域,我强烈建议阅读这个主题。同样在纸上画出你的问题真的有帮助。

EDIT2。

好吧,我想你的代码:

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}

+0

谢谢你的回复,但我不完全确定你的意思。不应该是与应用于rigidBody的相同旋转(现在有一个btVector3)?我尝试使用这个代码,但它导致刚体在上下移动时在它的质心上旋转。这个刚体是否必须具有0的高度才能起作用? – Jaitnium

+0

感谢您的更新。我明白你现在在做什么,但我仍然认为代码是不正确的。我打印出invTrans并且它看起来与bt-> offsetToPivot相同;所以由于某种原因,invRot没有得到正确应用。 – Jaitnium

+0

@Jaitnium你知道......我不能在任何地方运行这个代码,所以它是从我的脑海里写出来的,我无法检查它。如果你明白这个方法,你至少可以试着自己解决。你在哪一行打印这些值?你看到行'btPhys-> offsetToPivot = invTrans;'?在这条线后,他们应该是平等的。你的RigidBody的行为是什么?你将什么初始值分配给'offsetToPivot'?开始旋转时,最初的“WorldTransform”是什么?您是否按照我的建议尝试过使用调试器? – Estiny

1

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

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; 

即:

第1步:转换刚体原点。

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

步骤2:旋转在其质心的刚体。

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

步骤3:变换所述刚体关闭来源的。

+axisOfRotation.x; 
    +axisOfRotation.y; 

以下是处理你所需要的东西,并在矢量返回 所有连续旋转点递归函数:(用它作为基准)

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

+0

那么使用Bullet的意义在哪里?Btw。你的变换只能在2D平面上旋转一个'Point' ,而问题是围绕3D轴旋转一个'Rigid Body',它比这更复杂一点,即使你把它缩放到3D空间(这很痛苦),你仍然只计算身体,但方向在哪里? – Estiny

+0

@Esti你是对的。我会更新答案以符合问题要求,道歉! – Ziezi