围绕一个枢轴点旋转刚体
Rotating a RigidBody around a pivot point
我正在尝试围绕枢轴点(在本例中为原点)旋转刚体,而不是其质心。
我有一个建议,应用三个转换:
-
将刚体转换为原点
-
使刚体绕其质心旋转
-
使刚体脱离原点
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()));
然而,枢轴点似乎在四处移动,而不是停留在一个地方(原点)。是否有更好的方法(实际上是有效的)旋转刚体围绕一个枢轴点?
编辑:我为rotate函数添加了一些安全性检查代码:
//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 %fn", 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 %fn", 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();
变量轴是一个单位向量,表示对象围绕其旋转的轴。不是它的坐标系。我以前没有注意到这部分。四元数很棘手,你应该多读一些关于它们的知识,因为很多人都误用了它们。
在连续情况下工作的解决方案是将最后一次平移(从质心到枢轴-在我的例子中它由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}
在下面的函数中,这些转换执行您描述的三个步骤:
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) { }
};
为进一步阅读,解释背后的数学在这里
- 旋转播放器模型以指向一个点
- 在 cpp 中围绕原点旋转一个点?
- 将四元数旋转的游戏对象旋转另一个四元数时出现问题
- 在 OpenGL 中旋转一个正方形
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- 如何将对象围绕另一个对象(C 和OGRE3D)旋转
- CAS 是一个像旋转一样的循环吗?
- 用数学方法旋转一个有序数字数组
- 如何正确旋转一个矢量以匹配另一个矢量?(OPENGL)
- 围绕另一个 3D 点旋转一个 3D 点
- 旋转一个对象面对另一个C
- 旋转一个向量以达到与另一个向量的正交性
- 如何旋转一个对象使其面向另一个对象
- 在二维中仅旋转一个四边形
- 旋转一个不对称的截头体
- 旋转矩阵-围绕旋转的盒子旋转一个球
- 用OpenCV围绕一个点旋转一个点
- 在DirectX中围绕质心旋转一个等边三角形
- Opengl旋转一个已经在旋转的物体,就像地球月球旋转一样
- 用欧拉角旋转一个四元数