Original Post
Hi there, I really hope somebody can help me as I'm stumped! Basically, I am writing a little editor program where the user can click on a grid to change tiles. It is in 3D so I need to determine where the user has clicked on the plane that that the grid lies in. So, I wrote the following function: Basically, it's supposed to unproject the mouse coord at the camera position and at the far plane. This gives me a line which I can then use to intersect with the plane the grid lies on. This all works, but the line is slightly off, and seems more off at certain view angles. I'm actually drawing the line and it is definately not accuratly under the mouse. The result of this is that when using my editor, sometimes the wrong grid cell is selected! Something that obviously renders the editor useless! Does anybody have any idea what i may be doing wrong? If I can't figure this out the computer is going out the window! Cheers for any help!
void getIntersectPointPlane(D3DXVECTOR3* intersectPoint, int mouseX, int mouseY, D3DXPLANE* plane)
{
D3DXMATRIX matWorld, matView, matProj;
D3DXVECTOR3 Result1, Result2;
D3DVIEWPORT9 viewPort;
D3DXMatrixIdentity(&matWorld);
d3dDevice->GetTransform(D3DTS_VIEW, &matView);
d3dDevice->GetViewport(&viewPort);
d3dDevice->GetTransform(D3DTS_PROJECTION, &matProj);
D3DXVec3Unproject(&Result1, &D3DXVECTOR3((float)mouseX, (float)mouseY, 0.0f), &viewPort, &matProj, &matView, &matWorld);
D3DXVec3Unproject(&Result2, &D3DXVECTOR3((float)mouseX, (float)mouseY, 1.0f), &viewPort, &matProj, &matView, &matWorld);
D3DXPlaneIntersectLine(intersectPoint, plane ,&Result1, &Result2);
}