2013-07-16 21 views
0
 // get the current transform matrices 
    D3DXMATRIX matProjection, matView, matWorld, matInverse; 
    ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_PROJECTION, &matProjection); 
    ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_VIEW, &matView); 
    ENGINE.GetDevice()->GetDevice()->GetTransform(D3DTS_WORLD, &matWorld); 

    // use the mouse coordinates to get the mouse angle 
    float xAngle = (((2.0f * ENGINE.GetInputManager()->GetMousePos()->x)/WINDOW_WIDTH) - 1.0f)/matProjection(0, 0); 
    float yAngle = (((-2.0f * ENGINE.GetInputManager()->GetMousePos()->y)/WINDOW_HEIGHT) + 1.0f)/matProjection(1, 1); 

    D3DXVECTOR3 origin, direction; 
    origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); 
    direction = D3DXVECTOR3(xAngle, yAngle, 1.0f); 

    // find the inverse matrix 
    D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView)); 

    // convert origin and direction into model space 
    D3DXVec3TransformCoord(&origin, &origin, &matInverse); 
    D3DXVec3TransformNormal(&direction, &direction, &matInverse); 
    D3DXVec3Normalize(&direction, &direction); 

    // detect picking 
    BOOL hit; 
    std :: list<CEntity*> :: iterator iter = MAINMANAGER.GetLegoObjectManager()->GetObjList().begin(); 
    CResource* g_pResource = NULL; 

    while(iter != MAINMANAGER.GetLegoObjectManager()->GetObjList().end()) 
    { 
     g_pResource = static_cast<CEntity*>(*iter)->GetResource(); 

     D3DXIntersect(static_cast<PassiveMesh*>(g_pResource)->GetMesh(), &origin, &direction, &hit, NULL, NULL, NULL, NULL, NULL, NULL); 

     if(hit) 
     { 
      ENGINE.GetDevice()->GetDevice()->SetRenderState(D3DRS_LIGHTING, FALSE); 
      break; 
     } 
     else 
     { 
      ENGINE.GetDevice()->GetDevice()->SetRenderState(D3DRS_LIGHTING, TRUE); 
     } 

     ++iter; 
    } 

這是我採摘示例源代碼。 我有一個問題。詢問D3DXIntersect功能:(

,如果我創建了三個3D對象。所以挑選的作品第三個目的。不先工作,第二個對象。

,如果我只創建一個對象。採摘效果很好!

如果我創建了兩個對象。採摘工作第二個目的不是第一次。

只有c reate last obejct works picking well。

我不明白爲什麼會出現這個問題。

請給我一些建議。

+0

我不理解。你如何知道相交是否有效,如果只有在碰撞後才改變燈光,然後中斷?另外,你是什麼意思的seco?因爲這個列表顯然與您追蹤光線的位置無關。 –

回答

1

我猜測的是你的列表並不是按距離到相機的距離來排序的,這是正常的,但是因爲這個原因,你只需檢查一個網格,如果有一個打擊,你就打破了。相反,因爲您不知道列表的順序,您應該檢查列表中的所有網格,然後選取最短的網格。你現在做事情的方式使得挑選依賴於列表,而不是相機所在的位置。