[英]Camera following player opengl
我已經研究這個問題已有一段時間了,但找不到解決方案。 現在,我的相機正確地跟隨了玩家的位置,但是相機的旋轉發生了錯誤。 如果我只旋轉一圈,則可以正常進行,例如我僅沿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教程所取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.