Take the 2-minute tour ×

I was wondering how I would go about mapping 2D screen coordinates to a 3D world (specifically the xz plane) knowing:

-position of the camera

-equation of the screen plane

-equation of the xz plane

All I want to do is have the land on the xz plane light up when I hover the mouse over it.

Any help is greatly appreciated!


share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

If your world is rotated and shifted such that the camera is at x=y=z=0 (world coordinates), the world z coordinate increases away from the viewer (into the screen), and the projection plane is parallel to the screen and is at z=d (world coordinate; d > 0), then you determine screen coordinates from world coordinates this way:

xs = d * xw / zw
ys = d * yw / zw

And that's pretty intuitive: the farther the object from the viewer/projection plane, the bigger its zw and the smaller xs and ys, closer to the vanishing point of xw=yw=0 and zw=+infinity, which projects onto the center of the projection plane xs=ys=0.

By rearranging each of the above you get xw and zw back:

xw = xs * zw / d
zw = d * yw / ys

Now, if your object (the land) is a plane at a certain yw, then, well, that yw is known, so you can substitute it and get zw:

zw = d * yw / ys

Having found zw, you can now get xw by, again, substitution:

xw = xs * zw / d = xs * (d * yw / ys) / d = xs * yw / ys

So, there, given the setup described in the beginning and screen coordinates xs and ys of the mouse pointer (0,0 being the screen/window center), the distance between the camera and the projection plane d, and the land plane's yw you get the location of the land spot the mouse points at:

xw = xs * yw / ys
zw = d * yw / ys

Of course, these xw and zw are in the rotated and shifted world coordinates and if you want the original absolute coordinates in the "map" of the land, you un-rotate and un-shift them.

That's the gist of it.

share|improve this answer
I cannot thank you enough for this very comprehensive and understandable answer. –  user1170679 Apr 2 '12 at 8:54
A vote-up and accept would suffice. :) –  Alexey Frunze Apr 2 '12 at 9:05
Actually I have a question: my yw is known to be 0. Therefore given your equation: zw = d * yw / ys zw will always be equal to 0. How do I account for this? –  user1170679 Apr 2 '12 at 9:06
If ys=0 and you're looking from the point having its world y=0 (where your camera is in the setup), then you can't see the plane. ys must be nonzero if you want to see it. –  Alexey Frunze Apr 2 '12 at 9:10
Im not sure i understand your explanation. To flesh out my situation more clearly: -My camera is placed in the absolute middle of my projection plane which is the xy plane (zw=0) -My camera is 1000 pixels away from the projection plane (zw = -1000) -My terrain is the xz plane (yw=0) So, solving for xw for example we get xw = xs*0/ys = 0. I apologize for not understanding your last explanation :/ –  user1170679 Apr 2 '12 at 9:19
show 6 more comments

Your Answer


