gpt4 book ai didi

c++ - for 循环不能正常工作

转载 作者:行者123 更新时间:2023-11-27 23:32:19 24 4
gpt4 key购买 nike

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

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

最佳答案

您应该将您的for 语句修改为:

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

现在您编写 1>=i,这意味着 i 小于或等于 1。因此循环按照您编写的代码运行。

关于c++ - for 循环不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com