相机跟随玩家打开



我一直在研究这个问题一段时间,但我找不到解决方案。现在我的相机正确地跟随球员的位置,但相机的旋转出错了。如果我只使用一个旋转,它会正确运行,比如说我只沿 x 轴旋转,那么它工作正常。但是当我添加另一个旋转的第二个说"y"事情出错并且相机开始看向错误的方向时。

现在我的相机只有一个位置和一个旋转。

下面是一些代码。

glm::vec4 cameraPosition;
//This gives the camera the distance it keaps from the player.
cameraPosition.x = 0.0;
cameraPosition.y = 0.0;
cameraPosition.z = 20.0;
cameraPosition.w = 0.0;
// mStartingModel is the rotation matrix of the player.
glm::vec4 result = mStartingModel * cameraPosition;
//here I set the camera's position and rotation. The z rotation is given a extra 180 degrees so the camera is behind the player.
CameraHandler::getSingleton().getDefaultCamera()->setPosition(Vector3f(-player->mPosition.x + result.x, -player->mPosition.y + result.y, -player->mPosition.z + result.z), Vector3f(-(player->mRotation.x + 180), -player->mRotation.y, -player->mRotation.z) );

也知道我正在使用opengl,c ++和glm。

如果你想要一个简单的行为,使用gluLookAt可能是最简单的选择!更多信息在这里。

看到你使用glm,考虑类似

glm::mat4 CameraMatrix = glm::LookAt(
    cameraPosition, // the position of your camera, in world space
    cameraTarget,   // where you want to look at, in world space
    upVector        // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too
);

取自这个网站关于 opengl 教程。

最新更新