2012-01-16 92 views
1

我想綁定移動對象到按鈕按下。當我按下按鈕時,對象快速消失,看起來好像第一個翻譯一直在運行。然後,當我放開按鈕時,它會很快消失,最終會在沒有觸摸按鈕的情況下結束。當我按下/釋放按鈕時,在兩者之間彈起。DirectX 11 D3DXMatrixTranslation繼續運行?

D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix; 
bool result; 


// Generate the view matrix based on the camera's position. 
m_Camera->Render(); 

// Get the world, view, and projection matrices from the camera and d3d objects. 
m_Camera->GetViewMatrix(viewMatrix); 
m_Direct3D->GetWorldMatrix(worldMatrix); 
m_Direct3D->GetProjectionMatrix(projectionMatrix); 

// Move the world matrix by the rotation value so that the object will move. 
if(m_Input->IsAPressed() == true) { 
D3DXMatrixTranslation(&worldMatrix, 1.0f*rotation, 0.0f, 0.0f); 
} 
else { 
    D3DXMatrixTranslation(&worldMatrix, 0.1f*rotation, 0.0f, 0.0f); 
} 

// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. 
m_Model->Render(m_Direct3D->GetDeviceContext()); 

// Render the model using the light shader. 
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
           m_Model->GetTexture(), m_Light->GetDirection(), m_Light->GetDiffuseColor()); 


if(!result) 
{ 
    return false; 
} 

// Present the rendered scene to the screen. 
m_Direct3D->EndScene(); 

我還是DX11的新手,我已經看了一眼。我在這裏拉我的頭髮,試圖弄清楚發生了什麼事情。

回答

4

這就是你的代碼所做的。如果按下按鈕,則設置一個世界矩陣,如果不是 - 則爲另一個世界矩陣。你需要做的是將世界矩陣乘以一個新生成的翻譯矩陣。注意這個乘法每秒會發生〜60次,所以你只需要移動一個非常小的距離。

您的代碼應該是這樣的

if (m_Input->IsAPressed() == true) { 
    D3DXMATRIX translation; 
    D3DXMatrixTranslation(&translation, 0.05f, 0.0f, 0.0f); 
    worldMatrix *= translation; 
} 

你可能需要做

m_Direct3D->SetWorldMatrix(worldMatrix); 

或類似的東西。我不認爲我熟悉用於m_Camera和m_Direct3D的類。

+0

我對這一切仍然非常陌生。你可以發表一個示例代碼(或僞代碼?) – 2012-01-16 20:21:29

+1

「每個[幀]只移動一個非常小的距離」的一個相當重要的部分是使用幀時間來修改距離('unitsPerSecond * frameDelta')。這可以保持您的運動穩定並且不受幀率的影響。 – ssube 2012-01-17 11:21:52

+0

真的很有用。謝謝 – 2012-01-17 13:43:59