2013-10-29 75 views
1

我有以下功能代碼:雷採摘方向不正確計算

D3DXVECTOR3 Mouse::GetClickDirection(D3DXMATRIX projMatrix, D3DXMATRIX viewMatrix, D3DXVECTOR3 cameraPosition) 
{ 
    POINT mousePoint; 
    GetCursorPos(&mousePoint); 

    ScreenToClient(hwnd, &mousePoint); 

    float x = ((mousePoint.x * 2.0f)/(float)backBufferWidth) - 1.0f; 
    float y = ((mousePoint.y * -2.0f)/(float)backBufferHeight) + 1.0f; 

    D3DXVECTOR3 mouseClip = D3DXVECTOR3(x, y, 1.0f); 

    D3DXMATRIX invViewMatrix; 
    D3DXMatrixInverse(&invViewMatrix, 0, &viewMatrix); 

    D3DXMATRIX invProjMatrix; 
    D3DXMatrixInverse(&invProjMatrix, 0, &projMatrix); 

    D3DXMATRIX inversedMatrix = invViewMatrix * invProjMatrix; 

    D3DXVECTOR3 mouseWorldSpace; 
    D3DXVec3TransformCoord(&mouseWorldSpace, &mouseClip, &inversedMatrix); 

    D3DXVECTOR3 direction = mouseWorldSpace - cameraPosition; 

    D3DXVec3Normalize(&direction, &direction); 

    system("CLS"); 
    std::cout << "sX: " << x << std::endl; 
    std::cout << "sY: " << y << std::endl; 

    std::cout << "X: " << direction.x << std::endl; 
    std::cout << "Y: " << direction.y << std::endl; 
    std::cout << "Z: " << direction.z << std::endl; 

    return direction; 
} 

我試圖根據x和y的屏幕一點上,用戶點擊了在計算和創建一個定向光線我DirectX 3D應用程序。到目前爲止,印刷的結果似乎表明我的計算是錯誤的,因爲Z值總是在0.9左右,我不知道爲什麼。我在這裏做錯了什麼?請幫忙。謝謝

+0

你知道,還有就是[D3DXVec3Unproject功能(http://msdn.microsoft.com/en-us/library/windows/desktop/bb205526(V = vs.85)的.aspx),做基本上這個? –

回答

2

我認爲你的逆矩陣計算是倒退。

D3DXMATRIX inversedMatrix = invProjMatrix * invViewMatrix;