我在Opengl中展示字符,我使用四元数将它们绕轴旋转,我想实现的是让它们绕自己的轴旋转,但是当使用四元数时,如果我根据一个轴旋转对象,其他轴不会考虑这个旋转将绕固定的世界轴旋转。 给定一个四元数Qaccumulative(旋转),以及3个角度AngleX,AngleY和AngleZ。 首先,我认为欧拉旋转是要尝试的东西,我尝试了几种技术:
Quaternion rot1;
Quaternion rot2;
Quaternion rot3;
Vector3 axis ;
float angle;
QAccumulative.getAxisAngle(&axis, &angle);
// first try : around fix axes
cout << "axis : " << axis << endl;
rot1.FromAxis(Vector3(1.0,0.0,0.0),angleX);
rot2.FromAxis(Vector3(0.0,1.0,0.0),angleY);
rot3.FromAxis(Vector3(0.0,0.0,1.0),angleZ);
QAccumulative = QAccumulative * rot1;
QAccumulative = QAccumulative * rot2;
QAccumulative = QAccumulative * rot3;
/*
// second try, around current modified axes
rot1.FromAxis(Vector3(axis.x,0.0,0.0),angleX);
rot2.FromAxis(Vector3(0.0,axis.y,0.0),angleY);
rot3.FromAxis(Vector3(0.0,0.0,axis.z),angleZ);
QAccumulative = QAccumulative * rot1;
QAccumulative = QAccumulative * rot2;
QAccumulative = QAccumulative * rot3;
*/
/*
// third try with Euler rotation
Quaternion rotation;
rotation.FromEuler(10*angleX,10*angleY,10*angleZ);
QAccumulative = QAccumulative * rotation;
*/
QAccumulative.normalise();
迄今为止,它们都没有起作用......我不认为我的旋转实现有问题,但如果有人发现我会发布代码。欧拉不是我想象中的那样吗?我应该使用什么类型的旋转来实现我的目标?
编辑:我已经尝试过这个建议帖子中提到的方法:
rotate(float angleX,float angleY,float angleZ) {
Vector3 axis = Vector3(angleX,angleY,angleZ);
Vector3 worldAxis = QAccumulative * axis;
Quaternion worldRotation( worldAxis.x,worldAxis.y,worldAxis.z, 10 );
QAccumulative = worldRotation * QAccumulative;
QAccumulative.normalise();
角色仍然围绕固定轴旋转。
编辑: 我应用了一个帖子中提供的伪代码,它起作用了:
rotate(float angleX,float angleY,float angleZ) {
Vector3 axis = Vector3(angleX,angleY,angleZ);
Vector3 worldAxis = QAccumulative * axis;
Quaternion worldRotationx( 1.0,0,0, angleZ );
Quaternion worldRotationy( 0,1.0,0, angleX);
Quaternion worldRotationz( 0,0,1.0, -angleY );
QAccumulative = worldRotationx * worldRotationy * worldRotationz * QAccumulative;
QAccumulative.normalise();