第三人称相机翻转循环

时间:2013-07-12 08:25:19

标签: c++ opengl matrix

    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)
                       );

如你所见,我们建造了第三人称相机,这台相机正在追逐我们的飞机。但是当我们的飞机进行循环时,相机会翻转一半。所以一切都是颠倒的。我们怎样才能确保相机不会翻转?

1 个答案:

答案 0 :(得分:0)

我们通过计算而不是设置它来修复它。

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)
                       );