for 循环无法正常工作

发布于 2024-10-04 06:44:02 字数 2795 浏览 0 评论 0原文

我有一个检查两个对象的循环。它的问题是,它只检查第一个,但不检查其他...当我的循环检查第一个对象以进行拾取时,它会说它是否已被拾取,但是当它再次循环检查时第二个对象说它还没有被拾取,即使它被拾取了。所以我所做的就是切换检查过程。就像现在第二个对象被检查比第一个对象被检查一样。所以在我这样做之后,我得到这个结果,它说第二个对象是否被拾取,它工作正常,但是当它再次循环时,它开始检查第一个对象,它说即使它被拾取,它也没有被拾取..
这是我的循环

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:
好吧,我认为你们没有正确理解我的意思。我并不是想让我的循环检查更多实体
好的,我会的,这就是发生的事情。
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;
}

i have this loop that checks two objects. The problem with it, is that it only checks the first one but it doesn't check the others... When my loop check the first object for picking it says if it has been picked or not, but when it loops again to check the second object it says it has not been picked, even when it was picked. So what i did is that i switched the checking process. Like now the second object gets checked than the first object gets checked. So after i did that, i got this result it says that second object got picked or not, it works fine but when it loops again it start checking the first object it says that it is not picked even when it was picked..
here is my loop

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;
        }
    }

any idea?

EDIT 2:
ok i don't think you guys understood me right. I'm not trying to make my loop check more entity.
Ok i'll this is what happening.
1. When it loops for the first time it, checks to see if entity[0] is picked or not, This step works fine.
2. When it loops for the second time, checks to see if entity[1] is picked or not, HERE IS THE PROBLEM.
my loop works fine when it loops for the first time, but it doesn't work when it is looping for the second time.
When i was debugging i tried this.
1. When it loops for the first time it, checks to see if entity[1] is picked or not, This step works fine.
2. When it loops for the second time, checks to see if entity[2] is picked or not, HERE IS THE PROBLEM.
it seems that after the first loop there is something wrong, but i can't see what is it. By the way i don't get any errors.
EDIT 3:
The Entire Function

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-10-11 06:44:02

你应该将你的 for 语句修复为:

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

现在你写 1>=i,这意味着 i 小于或等于 1。所以循环按照您编码的方式工作。

you should fix your for statement to:

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

now you write 1>=i, it means i is less than or equals 1. So the loop works as you have coded it.

夜访吸血鬼 2024-10-11 06:44:02

我认为您必须重置 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;
    }
}

I think that you have to reset the value of the rayPos and rayDir structures, because you change the initial values inside the loop:

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;
    }
}
不甘平庸 2024-10-11 06:44:02

如果 hasHit 是您正在检查对象是否已被选取的变量,则看起来您当前正在检查特定于每个对象的变量。它似乎只是一个在 for 循环中永远不会更新的变量,并且始终保持相同的值。

If hasHit is the variable you are checking if the object has been picked, it doesn't look like you are currently checking a variable specific to each object. It appears just to be a variable that never gets updated in the for loop and will always hold the same value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文