Unprojecting 2D屏幕坐标的三维坐标 [英] Unprojecting 2D Screen Coordinates to 3D Coordinates
问题描述
我想知道我怎么会去映射2D屏幕坐标转换为三维世界(特别是XZ平面)了解:
I was wondering how I would go about mapping 2D screen coordinates to a 3D world (specifically the xz plane) knowing:
相机的 - 位
在屏幕平面 - 方程
在XZ平面的 - 方程
-equation of the xz plane
我想要做的是有在XZ平面上亮起的土地时,我悬停鼠标。
All I want to do is have the land on the xz plane light up when I hover the mouse over it.
任何帮助是极大AP preciated!
Any help is greatly appreciated!
谢谢!
推荐答案
如果你的世界是旋转移位,使得相机在 X = Y = Z = 0
(世界坐标),世界以Z
协调增加远离观察者(到屏幕),和投影平面平行于屏幕,是以Z = D
(世界坐标; D> 0
),然后确定从世界屏幕坐标坐标系是这样的:
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
这就是pretty的直观:从浏览器/投影平面更远的目标,更大的 ZW
和较小的 XS
和 YS
,接近 XW = YW = 0
和 ZW = +无限
,其投影到中央的投影平面 XS = YS = 0
。
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
.
通过以上你重新安排各获得 XW
和 ZW
回:
By rearranging each of the above you get xw
and zw
back:
XW = XS * ZW / D
ZW = D * YW / YS
现在,如果你的对象(土地)是平面上一定 YW
,然后,嗯,那 YW
是已知的,这样你就可以代替它,并获得 ZW
:
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
已经找到 ZW
,你现在可以得到 XW
通过再次替换:
Having found zw
, you can now get xw
by, again, substitution:
XW = XS * ZW / D = XS *(D * YW / YS)/ D = XS * YW / YS
因此,有鉴于在鼠标指针的开始和屏幕坐标 XS
和 YS
描述的设置(0,0是屏幕/窗口中心),相机和投影平面 D
,和之间的距离地平面的 YW
你拿到土地点的位置,鼠标点:
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
当然,这些 XW
和 ZW
在旋转和移动世界坐标,如果你想在原来的在地图土地的绝对坐标,您取消旋转和未转移他们。
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.
这是它的精神。
这篇关于Unprojecting 2D屏幕坐标的三维坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!