我希望在我的游戏中将我的汽车向左旋转90度.
当我使用这段代码时:
glm::quat rot(info.Rotation.w,info.Rotation.x,info.Rotation.y,info.Rotation.z); glm::quat done(glm::rotate(rot,glm::eulerAngles(rot)+glm::vec3(90.0f,0.0,0.0))); info.Rotation.x = done.x; info.Rotation.y = done.y; info.Rotation.z = done.z; info.Rotation.w = done.w;
汽车得到一个奇怪的旋转.
但是,以下代码根本不会改变汽车的旋转(正如我所期望的那样,只是为了确保GLM与游戏中的quats兼容):
glm::quat rot(info.Rotation.w,info.Rotation.z); glm::quat done(rot); info.Rotation.x = done.x; info.Rotation.y = done.y; info.Rotation.z = done.z; info.Rotation.w = done.w;
每当我试着检查旋转是否随之改变时:
glm::quat rot(info.Rotation.w,vec3(0.0,0.0))); info.Rotation.x = done.x; info.Rotation.y = done.y; info.Rotation.z = done.z; info.Rotation.w = done.w;
汽车轮换仅设置为游戏中的0,0轮换.我希望这个代码保持旋转不受影响,因为我期望以下代码将汽车向左旋转90度:
glm::quat rot(info.Rotation.w,vec3(90.0,0.0))); info.Rotation.x = done.x; info.Rotation.y = done.y; info.Rotation.z = done.z; info.Rotation.w = done.w;
但这不符合我的要求.它只是设置旋转,而不是将其添加到’rot’.
我究竟做错了什么?
解决方法
如果你不关心万向节锁,那么应该这样做.
glm::quat rot(info.Rotation.w,info.Rotation.z); glm::quat rot_euler_angles = glm::gtx::quaternion::eulerAngles(rot); rot_euler_angles.x += 90; glm::quat done(glm::rotate(rot,rot_euler_angles)); info.Rotation.x = done.x; info.Rotation.y = done.y; info.Rotation.z = done.z; info.Rotation.w = done.w;
我认为这也是有效的
glm::vec3 rot(90.0*(float)M_PI/180.0,0); info.Rotation = glm::normalize(info.Rotation * glm::quat(rot));
四元数很棒,因为它们可以复合以进行非常复杂的旋转.