四元数和旋转

GLM - Quaternions and Rotation

本文关键字:旋转 四元数      更新时间:2023-10-16

我试图使用四元数,以便在我的程序中旋转对象。

但是他们的问题是……一个旋转。我试着这样旋转矩阵:

 ModelMatrix = ModelMatrix * glm::toMat4(glm::quat(RotationW, RotationX, RotationY, RotationZ));

与随机数,结果是一个巨大的缩放(为什么)和一点点旋转。

另外我们有:

quat3 = quat2 * quat1
Model1 = Model * glm::toMat4(glm::quat( quat3.w , quat3.x, quat3.y, quat3.z);
Model2 = Model * glm::toMat4(glm::quat( quat1.w , quat1.x, quat1.y, quat1.z);
Model2 = Model2 * glm::toMat4(glm::quat( quat2.w , quat2.x, quat2.y, quat2.z); 

Model2和Model1是否具有相同的旋转?

我知道glm::rotate(Model,RotationAngle, RotationAxis),但我宁愿使用四元数

为了使四元数表示纯旋转,它必须是单位四元数。随机数通常没有这个属性。您可能希望在生成四元数之前对它们进行规范化:

l = sqrt(RotationW * RotationW + RotationX * RotationX
         + RotationY * RotationY + RotationZ * RotationZ);
RotationW /= l;
RotationX /= l;
...

当然,仅仅将四元数转换为矩阵是没有意义的。直接计算矩阵会更有效率。

对于四元数乘积,您的顺序错了:

q3 = q2 * q1
=> M * mat(q3) = M * mat(q2) * mat(q1)