float FoV = initialFoV - 5;
//(*it)->getParent()->getPosition() + (*it)->getOrientationQuat() * (*it)->getPosition();
glm::vec3 lookAt = carPosition;
glm::vec3 temp;
temp.x = spaceShip->orientation.y;
temp.y = spaceShip->orientation.x;
temp.z = spaceShip->orientation.z;
glm::vec3 cameraposition = carPosition + glm::quat(temp) * position;
ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
ViewMatrix = glm::lookAt(
cameraposition, // Camera is here
lookAt, // and looks here : at the same position, plus "direction"
vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
如您所见,我们构建了一个第三人称相机,这个相机正在追逐我们的飞机。但是当我们的飞机做一个循环时,相机会在中途翻转。所以一切都是颠倒的。我们如何确保相机不会翻转?
最佳答案
我们通过计算 up 而不是设置它来修复它。
glm::vec3 cameraposition = carPosition + glm::quat(temp) * position;
ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 RotationMatrix = eulerAngleYXZ(carDirection.x, carDirection.y, carDirection.z);
glm::vec4 up = RotationMatrix * glm::vec4(0,1,0,0);
glm::vec3 up3(up);
ViewMatrix = glm::lookAt(
cameraposition, // Camera is here
lookAt, // and looks here : at the same position, plus "direction"
up3 // Head is up (set to 0,-1,0 to look upside-down)
);
关于c++ - 第三人称相机翻转循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17610622/