2012-12-09 62 views
0

最近我一直在試圖在DirectX 9中製作一個好的工作相機,但我遇到了問題。所以,讓我告訴你一些我的代碼。我不使用D3DXMatrixLookAtLH函數,因爲我也想旋轉相機。C++ DirectX FPS相機奇怪

D3DXMATRIX matView, 
      matVTranslate, 
      matVYaw, 
      matVPitch, 
      matVRoll; 

D3DXTranslation(&matVTranslate, x, y, z); 
D3DXRotationY(&matVYaw, yaw); 
D3DXRotationX(&matVPitch, pitch); 
D3DXRotationZ(&matVRoll, roll); 

matView = matVTranslate * (matVPitch * matVYaw * matVRoll); 

d3ddev->SetTransform(D3DTS_VIEW, &matView); 

它創造了一個非常奇怪的效果。有沒有更好的方法來創建一個fps相機?如果你想運行該程序,這裏是exe文件。 The Exe如果你想要的代碼,請讓我知道。謝謝。

回答

3

即使是fps,您也可以輕鬆使用D3DXMatrixLookAtLHdoc)。角色的眼睛位置是pEye。對於視圖的旋轉,您可以保存一個向量,該向量包含視向的標準化向量。這個你可以用你的旋轉矩陣進行轉換,將它添加到pEt的pEye中。

D3DXMATRIX matView, 
      matLook; 

D3DXMatrixRotationYawPitchRoll(&matLook,pitch,yaw,roll); 

D3DXVector3 lookdir; 
// yaw,pitch,roll are assumed to be absolute, for deltas you must save the lookdir 
lookdir = D3DXVector3(0.0,0.0,1.0); 
D3DXVec3TransformCoord(&lookdir,&lookdir,&matLook); 

D3DXVector3 at; 
at = camerapos + lookdir; 

D3DXMatrixLookAtLH(&matView,&camerapos,&at,&up); 

d3ddev->SetTransform(D3DTS_VIEW, &matView); 
+0

謝謝,但你能告訴我,在代碼中,因爲我做了太多的研究,每天都厭倦它。謝謝。 –

+0

我也是一個初學者,但一個非常快的學習者 –

+0

我已經添加了一些示例代碼。我沒有測試過,但一般的想法應該清楚,希望:) – Gnietschow