【问题标题】:Camera following player opengl相机跟随播放器opengl
【发布时间】:2012-11-16 13:38:45
【问题描述】:

我一直在研究这个问题一段时间,但我找不到解决方案。 现在我的相机正确地跟随玩家的位置,但是相机的旋转出错了。如果我只使用一次旋转,它会正确运行,比如我只沿 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。

【问题讨论】:

    标签: c++ opengl camera rotation glm-math


    【解决方案1】:

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

    看到你使用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 tutorials

    【讨论】:

    • 您好,谢谢您的回答,但由于某种原因它不适用于我的相机,我猜我的相机和这台相机不兼容。我想我可以从代码中删除我的相机并使用它,但如果有一种更简单的方法可以让我保留我的相机,那就太好了。
    • 也许你应该定义什么是“你的相机”?我不熟悉这个引擎,但我过去写过相机系统,所以我可能可以提供帮助
    • 现在它是一个简单的类,它只保存相机的位置和旋转,并具有一些设置功能。这就是现在的一切。老实说,这部分是一个朋友给我的,我现在怀疑是否应该保留它。
    • 如果它所做的只是保持位置和旋转,你可能可以摆脱它:)
    • 是的,我是这么想的。 DX 现在将不得不重写相当多的东西。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多