• Advertisement
Sign in to follow this  

Terrain picking / mesh intersection

This topic is 4652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I got some trouble with the function D3DXIntersect. It works fine when I use some testing vectors. But when I calculate the start and end vectors of my mouse click position, calculate the direction from start to end and use that in the Intersect function, I don't get the right outcome. I draw a line between the start and end vector to see how the ray looks like and there are no problems there. The ray is right how it suppose to be. I calculate the Direction of the ray by subtracting the start vector from the end vector. This is the source, the CursorX and CursorY are the screen coordinates where I click:
void Intersect(int CursorX, int CursorY)
{
	D3DVIEWPORT9 ViewPort;
	g_pd3dDevice->GetViewport(&ViewPort);

	D3DXVECTOR3 Start	= D3DXVECTOR3((float)Cursor.x, (float)Cursor.y, 0.0f);
	D3DXVECTOR3 End		= D3DXVECTOR3((float)Cursor.x, (float)Cursor.y, 1.0f);
	D3DXVECTOR3 Direction;

	D3DXVec3Unproject(&Start, &Start, &ViewPort, &Projection, &View, &World);
	D3DXVec3Unproject(&End, &End, &ViewPort, &Projection, &View, &World);
		
	Direction = End - Start;
	D3DXVec3Normalize(&Direction, &Direction);
        BOOL Hit = FALSE; DWORD Face = 0, Count = 0;

	D3DXIntersect(g_pMesh[0], &Start, &Direction, &Hit, &Face, NULL, NULL, NULL, NULL, &Count);
	if(Hit)
	{
            // Do something here! 
	}

Does anyone see the problem here, what am I forgetting to do to make it work? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
After some more testing, I saw the function does what it supposed to do, I was drawing multiple meshes, that weren't checked on intersection :S.

I solved the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement