2012-11-16 40 views
0

我一直在調查這個問題一段時間,但我找不到解決方案。 現在我的相機正確地跟隨了玩家的位置,但是相機的旋轉出錯了。如果我只使用一個旋轉它正確地說,我只沿着X軸旋轉,然後它工作正常。但第二個我再添加一個旋轉說,'Y'的事情出錯了,相機開始尋找錯誤的方向。攝像頭下面的播放器opengl

現在我的相機只有一個位置和一個旋轉。

這是一些代碼。

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。

回答

3

如果你想要一個簡單的行爲,使用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

+0

嘿謝謝你的回答,但對於某些原因,它不適用於我的相機,我猜我的相機和本相機不兼容。我想我可以將我的相機從代碼中剪下來,然後使用它,但是如果有一種方法可以讓我的相機拍照,那麼這將會很好。 –

+0

也許你應該定義「你的相機」是什麼?我對這個引擎不熟悉,但是我過去寫過相機系統,所以我可以幫助 – emartel

+0

好了,現在它只是一個簡單的類,它只保存相機的位置和旋轉,並具有一些設置的功能。這就是現在。老實說,這部分是由一位朋友給我的,我現在懷疑我是否應該做到這一點。 –