2012-03-28 111 views
0

我有多個物體在3D空間中移動,並且正在尋找方法,在按下按鈕時,讓相機捕捉並跟隨所選對象。基於3D模型的世界矩陣的DirectX相機?

有沒有辦法利用每個對象的worldMatrix? (下面是一個對象的例子,這一個是旋轉&軌道的行星)

//set up matrices for rendering 
D3DXMATRIX worldMatrixMer, viewMatrixMer, projectionMatrixMer; 
m_Camera->GetViewMatrix(viewMatrixMer); 
m_D3D->GetWorldMatrix(worldMatrixMer); 
m_D3D->GetProjectionMatrix(projectionMatrixMer); 
D3DXMatrixRotationX(&matRotateX, rx/65.0f); 
//Rotate about Y axis 
D3DXMatrixRotationY(&matRotateY, rotation * 15.0f); 
D3DXMatrixRotationZ(&matRotateZ, rz/65.0f); 
//Collate Rot Matrices 
D3DXMATRIX rotMatrixMer = matRotateX * matRotateY * matRotateZ; 
D3DXVECTOR3 newVecDirMer; 
D3DXVec3TransformCoord(&newVecDirMer, &initVecDirMer, &rotMatrixMer); 
D3DXVec3Normalize(&currentVecDirMer, &newVecDirMer); 
//Create the size of the object 
D3DXMATRIX matScaleMer; 
D3DXMatrixScaling(&matScaleMer, 0.1f, 0.1f, 0.1f); 
//Starting position of object 
D3DXMatrixTranslation(&matTranslateMer, 0.0f, 0.0f, 3.5983f * 3); 
//Rotate about it's own axis 
D3DXMatrixRotationY(&worldMatrixMer, rotation); 
worldMatrixMer *= rotMatrixMer * matScaleMer * matTranslateMer; 
//'Orbit' 
D3DXMatrixRotationY(&matOrbit, (-1000.0f * rotation)/88); 
worldMatrixMer *= matOrbit; 

我真的試圖找到使這種情況發生的一個優雅的方式,因此任何建議,將不勝感激。

感謝

回答

1

有沒有一種方法可以利用每個對象的worldMatrix?

對象矩陣可以表示這樣:

objx.x  objx.y  objx.z 0 //m[0][0]..m[0][3] or _11, _12, _13, _14 
objy.x  objy.y  objy.z 0 //m[1][0]..m[1][3] or _21, _22, _23, _24 
objz.x  objz.y  objz.z 0 //m[2][0]..m[2][3] or _31, _32, _33, _34 
objpos.x objpos.y objpos.z 1 //m[3][0]..m[3][3] or _41, _42, _43, _44 

m[][]_11 .. _44對應的D3DMATRIX元件,objpos - 轉化的對象x('局部x」 - 對象位置矢量,物objx到世界空間)載體,等等

所以只要最後一列(M [0..3] [3])爲0,0,0,1可以提取對象的位置和它的 「×」, 「y」,「z」向量(「side」,「up」,「front」 - 這取決於appli陽離子)。如果最後一列不是「0,0,0,1」,那麼它就是投影矩陣,您不能從中輕鬆提取對象數據。

從那裏,你可以做任何你想用你的相機。爲打破矩陣爲我

+0

謝謝你,這可能是我會去的方式。 – 2012-03-29 08:58:19

0

我將通過攝像機的位置設置爲相同的對象處理這個要遵循。如果你的對象被保存在一個向量中,只需將向量中的索引遞增到下一個對象,就可以更容易地改變要追蹤的對象。將對象的位置應用到相機的世界位置後,應該按照相機的偏移量調用MatrixTranslation。這些將是值得玩的好處。相機的lookat元素應該等於物體的位置,以便相機直接看它。如果你不想讓相機直接看物體,那麼也可以翻譯它。最後,相機上的向上矢量應該指向當然。

希望這會有所幫助。