Im have been looking for information on coverting screen coordinates to
world coordinates (in DirectX, but I’m mainly looking for API
independant info). I have found a tutorial for converting them in
DirectX (Clicky) but
it seems to not work. (The code produces points in world space, but they
dont line up with where im clicking at all… unless I’m directly in the
centre of the world!)
Does anyone have any pointers/advice on this, as it seems to be a topic
thats a bit hard to come by via Google…
Please log in or register to post a reply.
this seems pretty straight forward ->
Have you already seen that?
basically you just need to transform your screen space coordinates to
normalized coordinates (-1 to 1 with the origin at the center of the
half_width = width * 0.5f
half_height = height * 0.5f
aspect_ratio = width / height;
dx = (x / half_width - 1.0f) / aspect_ratio;
dy = 1.0f - y / half_height
then you need to find the rotation axis (axi ? axen ?) (this is just for
clarity… one can obviously do better than this)
m = camera_matrix;
xr = m * Vector3(1.0, 0.0, 0.0);
yr = m * Vector3(0.0, 1.0, 0.0);
zr = m * Vector3(0.0, 0.0, 1.0);
xr *= aspect_ratio;
zr *= -(0.5 * aspect_ratio * tan(0.5 * field_of_view));
finally you can compute a ray through the normalized coordinates
computed earlier :
ray = (m.GetTranslation(), dx * xr + dy * yr + zr);
OK, I think I am finallly there…
I took someones advice and used the D3DX Unproject functions to get a
head start, and now I have it workign without these functions so thanks
Its not API free yet, as my Matrix Inverse function is a bit faulty at
the mo, so I’m still having some trouble!
Thanks for the help guys
Just reverse the process that takes the point from world to screen
space… not too hard eh?
I.e., instead of World->View->Clip->Screen go
its not as simple as that. You also have to take into account that when
you go the reverse process, you’re missing the z-coordinate.