Sign in to follow this  
LandonJerre

D3DXIntersect and D3DXBoxBoundProbe not works correctly

Recommended Posts

Hello. I have some problem with D3DXIntersect and D3DXBoxBoundProbe in my "collision detection" code. Here's the code:
void CFactory::Intersect(D3DXVECTOR3 pRayPos,D3DXVECTOR3 pRayDir)
{
	vector <XMesh>::iterator Iter;
	BOOL bHit = FALSE;
	bool IsHit = false;
	float dist = 0;
	float smallestdist = 1000000;
	vector <XMesh>::iterator EraseIter;
	for(Iter = MeshContainer.begin(); Iter != MeshContainer.end();Iter++)
	{
		if(!(Iter->Options&FO_NOINTERSECTTEST)&&!(Iter->NoRender))
		{
			D3DXVECTOR3 LocalRayPos;
			D3DXVECTOR3 LocalRayDir;
			D3DXMATRIX InverseWorldMatrix;
			D3DXMatrixInverse(&InverseWorldMatrix,NULL,
				&(Iter->WorldMatrix));
			D3DXVec3TransformCoord(&LocalRayPos,&pRayPos,&InverseWorldMatrix);
			D3DXVec3TransformNormal(&LocalRayDir,&pRayDir,&InverseWorldMatrix);
			/*D3DXIntersect(Iter->MeshData,&pRayPos,&pRayDir,&bHit,NULL,NULL,NULL,
				&dist,NULL,NULL);*/
			bHit = D3DXBoxBoundProbe(&Iter->BBox.Min,&Iter->BBox.Max,
				&LocalRayPos,&LocalRayDir);
			dist = D3DXVec3Length(&LocalRayPos);
			if(bHit && dist<smallestdist)
			{
				smallestdist = dist;
				EraseIter = Iter;
				IsHit = true;
			}
		}
	}

	if(IsHit)
	{
		EraseIter->NoRender = true;
	}
}


As you can see I store the mesh data in a STL vector container. (NoRender-bool; WorldMatrix-D3DXMATRIX;BBox.Min/BBox.Max D3DXVECTOR3 (Bounding Box)). The problem : For example I have a sphere which is translated from D3DXVECTOR3(0,0,0) to D3DXVECTOR3(5,0,5) with his world matrix. If pRayPos=D3DXVECTOR3(3,0,3) and pRayDir=D3DXVECTOR3(4,0,4) no problem, works correct. But if pRayPos=D3DXVECTOR3(7,0,7) and pRayDir=D3DXVECTOR3(4,0,4) bHit is false which means there is no intersection between the mesh and the ray. I've tried with D3DXBoxBoundProbe and D3DXIntersect too but the result was the same. I hope you can understand me (I can't really speak/write English)and you can help me. Thanks.

Share this post


Link to post
Share on other sites
Any ideas? If you need more info just say. (I'm using Visual Studio 2005 Express Edition and DX SDK 2006 February) I'm really need your help. I'm trying to resolve it since Friday but I can't find out what cause the problem.

Share this post


Link to post
Share on other sites
Well, your code looks good. If the sphere is at (5, 0 ,5) and you have a pick ray at position (7, 0, 7) with direction (4, 0, 4), isn't the starting position of the ray above and beyond the sphere, with the direction going further up and away? Are you expecting the ray's position to be inside the sphere? If the sphere has a radius smaller than 2, the ray won't be inside it or hit it.

Share this post


Link to post
Share on other sites
Quote:
Original post by DXnut
Well, your code looks good. If the sphere is at (5, 0 ,5) and you have a pick ray at position (7, 0, 7) with direction (4, 0, 4), isn't the starting position of the ray above and beyond the sphere, with the direction going further up and away? Are you expecting the ray's position to be inside the sphere? If the sphere has a radius smaller than 2, the ray won't be inside it or hit it.

Then I think I misunderstood the working mechanism of these functions. Thanks for the information and sorry for my stupid question.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this