2010-11-25 183 views
0

我有這個循環檢查兩個對象。它的問題在於,它只檢查第一個,但它不檢查其他...當我的循環檢查第一個對象選擇它說如果它已被選中或沒有,但當它再次循環檢查它說它沒有被選中的第二個對象,即使它被選中。所以我做的是我切換了檢查過程。像現在一樣,第二個對象被檢查,而第一個對象被檢查。所以,當我這樣做後,我得到了這個結果它說,第二個對象被選中或沒有,它工作正常,但當它再次循環時,它開始檢查第一個對象,說它不被挑選,即使當它被挑選..
這裏是我的循環for循環不能正常工作

for(int i=0; 1>=i; i++) 
    { 
     matWorld=entity[i]->s; 
     // Use inverse of matrix 
     D3DXVec3Unproject(&rayPos,&rayPos,&vp,&matProj,&matView,matWorld); 
     D3DXVec3Unproject(&rayDir,&rayDir,&vp,&matProj,&matView,matWorld); 
     rayDir -= rayPos; // make a direction from the 2 positions 
     D3DXVec3Normalize(&rayDir,&rayDir); 

     if(FAILED(D3DXIntersect(entity[i]->pDrawMesh, &rayPos, &rayDir, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL))) 
     { 
      PostQuitMessage(0); 
     }; 

     if(hasHit!=0) 
     { 
      entity[i]->draw=false; 
     } 
    } 

任何想法?

編輯2:
好吧,我不認爲你們明白我的權利。我不想讓我的循環檢查更多entity
好吧,我會這樣發生。
1.當它第一次循環時,檢查entity[0]是否被選中,這一步工作正常。
2.當它第二次循環時,檢查entity[1]是否被選中,這是問題所在。
我的循環在第一次循環時工作正常,但它在第二次循環時不起作用。
當我調試我試過這個。
1.當它第一次循環時,檢查entity[1]是否被選中,此步驟工作正常。
2.當它第二次循環時,檢查是否挑選entity[2],這是問題所在。 似乎在第一次循環後出現了錯誤,但我看不到它是什麼。順便說一句,我沒有得到任何錯誤。 編輯3: 整函數

BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity[4]) 
{ 
    D3DXMATRIX matProj; 
    POINT pt; 
    D3DVIEWPORT9 vp; 
    D3DXMATRIX *matWorld=NULL; 
    D3DXMATRIX matView; 

    GetCursorPos(&pt); 
    ScreenToClient(hWnd, &pt); 
    d3ddev->GetTransform(D3DTS_PROJECTION, &matProj); 
    d3ddev->GetViewport(&vp); 
    d3ddev->GetTransform(D3DTS_VIEW, &matView); 

    D3DXVECTOR3 rayPos((float)pt.x, (float)pt.y,0); // near-plane position 
    D3DXVECTOR3 rayDir((float)pt.x, (float)pt.y,1); // far-plane position 

    BOOL hasHit; 
    float distanceToCollision; 
    for(int i=0; i<=1; i++) 
    { 
     matWorld=entity[i]->s; 
     // Use inverse of matrix 
     D3DXVec3Unproject(&rayPos,&rayPos,&vp,&matProj,&matView,matWorld); 
     D3DXVec3Unproject(&rayDir,&rayDir,&vp,&matProj,&matView,matWorld); 
     rayDir -= rayPos; // make a direction from the 2 positions 
     D3DXVec3Normalize(&rayDir,&rayDir); 

     if(FAILED(D3DXIntersect(entity[i]->pDrawMesh, &rayPos, &rayDir, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL))) 
     { 
      PostQuitMessage(0); 
     }; 

     if(hasHit!=0) 
     { 
      entity[i]->draw=false; 
     } 
    } 

    return hasHit; 
} 
+3

這樣的描述是真的很難,沒有標點符號閱讀。 – BBoy 2010-11-25 20:17:02

+0

確定即時添加他們現在對不起 – Ramilol 2010-11-25 20:18:31

+3

這是......一個句子? – jwueller 2010-11-25 20:18:46

回答

1

我認爲你必須重置rayPos和rayDir結構的價值,因爲你改變了初始值內循環:

for(int i=0; i<=1; i++) 
{ 
    matWorld=entity[i]->s; 
    // Use inverse of matrix 
    D3DXVECTOR3 rayPos((float)pt.x, (float)pt.y,0); // near-plane position 
    D3DXVECTOR3 rayDir((float)pt.x, (float)pt.y,1); // far-plane position 
    D3DXVec3Unproject(&rayPos,&rayPos,&vp,&matProj,&matView,matWorld); 
    D3DXVec3Unproject(&rayDir,&rayDir,&vp,&matProj,&matView,matWorld); 
    rayDir -= rayPos; // make a direction from the 2 positions 
    D3DXVec3Normalize(&rayDir,&rayDir); 

    if(FAILED(D3DXIntersect(entity[i]->pDrawMesh, &rayPos, &rayDir, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL))) 
    { 
     PostQuitMessage(0); 
    }; 

    if(hasHit!=0) 
    { 
     entity[i]->draw=false; 
    } 
} 
2

你應該解決您for聲明:

int size = ... // detect the size of entity 
for(int i=0; i <= size; i++) 

現在你寫1>=i,這意味着i小於或等於1所以循環按照您編碼的方式工作。

1

如果hasHit是您檢查對象是否被選中的變量,它看起來不像您當前正在檢查特定於每個對象的變量。它似乎只是一個變量,永遠不會在for循環中更新,並且始終保持相同的值。