简体   繁体   English

从3D位置和角度计算2D表面坐标

[英]Calculate 2D surface cooridante from 3D position and angle

I have a 3D scene with a movable camera. 我有一个带有可移动相机的3D场景。 I have the 3D coordinates for that camera (x, y, z -> Y being the height) and the X and Y rotation (Up/Down, Left/Right). 我有该摄像机的3D坐标(x,y,z-> Y为高度)以及X和Y旋转(上/下,左/右)。

I want get the coordinates (x1, z1) in the floor where I'm looking at. 我想获得我正在看的地板上的坐标(x1,z1)。

Basically if the camera is at (0, 4096, 0) (4096 is the height) and my xRotation is 45º and my yRotation is 0, I will be looking at the point on the floor (4096, 0, 0) 基本上,如果摄影机位于(0,4096,0)(高度为4096),并且xRotation为45º而yRotation为0,我将查看地板上的点(4096,0,0)

I was trying to program it but i got stuck with the trigonometry. 我试图对其进行编程,但是我被三角学困住了。 Help me with it. 帮帮我。

The following code is what I have right now and not fully working: 以下代码是我现在所拥有的并且不能完全正常工作:

float x1, z1, anguloX, anguloY;
anguloX = (90 - Xrotation) / 180 * Pi;
anguloY = (90 - Yrotation) / 180 * Pi;

x1 = Yposition * tan(anguloX) * cos(anguloY);
z1 = Yposition * tan(anguloY) * cos(anguloY);

x1 += Xposition;
z1 += Zposition;

Do not bother yourself with this kind of trigonometry, It is often more practical and understandable to use matrices and transformation to do this kind of stuff. 不要为这种三角而烦恼,使用矩阵和变换来做这种事情通常更实用,也更容易理解。 I suggest to try this approach instead which is called Ray Casting: 我建议尝试使用这种方法,即Ray Casting:

  1. Calculate the direction of camera sight, you must get the looking direction of camera 计算摄像机视线的方向,您必须获取摄像机的视线方向
  2. Use ray casting to determine the intersection point of sight ray and scene meshes(or just a plane that represents the floor). 使用射线投射确定视线和场景网格物体(或只是代表地板的平面)的交点。

Now about the first, here is the pseudo code : 现在关于第一个,这是伪代码:

XRotMat = CreateRotationMatrixAroundXAxis(verticalCameraAngel);
YRotMat = CreateRotationMatrixAroundYAxis(horizentalCameraAngel);

CameraSightDir = YRotMat*XRotMa*initialCameraDir;

and about the second step: 关于第二步:

SightRay.Source = Camera.Position;
SightRay.Direction = CameraSightDir;

Intersection = IntersectRayWithPlane(SightRay , FloorPlane);

IntersectRayWithPlane is quite a simple procedure, you can read about it here . IntersectRayWithPlane是一个非常简单的过程,您可以在此处阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM