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!
Thanks!
If your world is rotated and shifted such that the camera is at
x=y=z=0(world coordinates), the worldzcoordinate increases away from the viewer (into the screen), and the projection plane is parallel to the screen and is atz=d(world coordinate;d > 0), then you determine screen coordinates from world coordinates this way:xs = d * xw / zwys = d * yw / zwAnd that’s pretty intuitive: the farther the object from the viewer/projection plane, the bigger its
zwand the smallerxsandys, closer to the vanishing point ofxw=yw=0andzw=+infinity, which projects onto the center of the projection planexs=ys=0.By rearranging each of the above you get
xwandzwback:xw = xs * zw / dzw = d * yw / ysNow, if your object (the land) is a plane at a certain
yw, then, well, thatywis known, so you can substitute it and getzw:zw = d * yw / ysHaving found
zw, you can now getxwby, again, substitution:xw = xs * zw / d = xs * (d * yw / ys) / d = xs * yw / ysSo, there, given the setup described in the beginning and screen coordinates
xsandysof the mouse pointer (0,0 being the screen/window center), the distance between the camera and the projection planed, and the land plane’sywyou get the location of the land spot the mouse points at:xw = xs * yw / yszw = d * yw / ysOf course, these
xwandzware 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.