Quote:
It's a simple matter of determining a point in 3D space
Yes! I'm asking how can I find the (x,y) 3D world coordinates from a given 2D mouse point and a given 3D Z value...
[input]
2D mouse point
Z value
[output]
The 3D World coordinates in which I've to put my object, so the user sees it under the mouse
The code you've posted is right (only now I understand it).
POINT mousePt;D3DXVECTOR3 v0, v1;D3DXVECTOR3 rayPos, rayDir;v0 = D3DXVECTOR3(mousePt.x, mousePt.y, 0);v1 = D3DXVECTOR3(mousePt.x, mousePt.y, 1);D3DXVec3Unproject(&rayPos, &v0, &vp, &proj, &view, &world);D3DXVec3Unproject(&rayDir, &v1, &vp, &proj, &view, &world);rayDir -= rayPos;D3DXVec3Normalize(&rayDir,&rayDir);// rayDir is a vector in 3D world from eye point to infinity// define D3DXVECTOR3 wPos = rayPos+factor*rayDir with wPos a point in the plane// you want to solve for factor// wPos.z = rayPos.z + factor*rayDir.z// wPos.z = objectZCoord // since it's in the plane// CHECK FOR RAYDIR.Z==0 to avoid division by zero// This happens when you're looking parallel to the plane// factor = (wPos.z - rayPos.z)/rayDir.z// wPos.x = rayPos.x + rayDir.x*factor // desired x value// wPos.y = rayPos.y + rayDir.y*factor // desired y value
The issue is related to that damned "worldMatrix" :-).
Really, I don't understand why I've to put an Identity matrix instead of the matrix that describes the world in which I'd put the object.
Please be patient with me, I'm only an unexpert DirectX developer and all these stuff are getting me crazy.
Again, thank you for your help.