2
我正在嘗試在glm :: quat中定義的方向翻譯相機的位置。使用glm沿着四元數翻譯相機
void Camera::TranslateCameraAlongZ(float distance) {
glm::vec3 direction = glm::normalize(rotation * glm::vec3(0.0f, 0.0f, 1.0f));
position += direction * distance;
}
當旋轉是身份四元時,這很好地工作。當旋轉是別的東西時,X和Z翻譯不起作用。當相機向左旋轉45度時,我打電話給TranslateCameraAlongZ(-0.1f)
,當我應該向後和向右移動時,我被向後翻譯並向左翻譯。所有不是完美90度增量的翻譯都是相似的。我在這裏做錯了什麼,修復它的最簡單方法是什麼?在情況下,他們可能是相關的,這裏是產生我的觀點矩陣旋轉我的相機的功能和作用:
glm::mat4 Camera::GetView() {
view = glm::toMat4(rotation) * glm::translate(glm::mat4(), position);
return view;
}
void Camera::RotateCameraDeg(float x, float y, float z) {
rotation = glm::normalize(glm::angleAxis(x,glm::vec3(1.0f, 0.0f, 0.0f)) * rotation);
rotation = glm::normalize(glm::angleAxis(y,glm::vec3(0.0f, 1.0f, 0.0f)) * rotation);
rotation = glm::normalize(glm::angleAxis(z,glm::vec3(0.0f, 0.0f, 1.0f)) * rotation);
std::cout << glm::eulerAngles(rotation).x << " " << glm::eulerAngles(rotation).y << " " << glm::eulerAngles(rotation).z << "\n";
}