0
101 Feb 02, 2005 at 12:22

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…

Thanks
Spree

#### 5 Replies

0
101 Feb 02, 2005 at 15:15

this seems pretty straight forward -> http://www.toymaker.info/Games/html/picking.html

Have you already seen that?

0
101 Feb 02, 2005 at 15:21

basically you just need to transform your screen space coordinates to normalized coordinates (-1 to 1 with the origin at the center of the viewport).

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);

0
101 Feb 02, 2005 at 20:23

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 guys :)

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

Spree

0
101 Feb 10, 2005 at 08:26

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 Screen->Clip->View->World.

0
101 Feb 10, 2005 at 10:08

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.