【问题标题】:Calculate 2D surface cooridante from 3D position and angle从 3D 位置和角度计算 2D 表面坐标
【发布时间】:2014-06-04 16:15:17
【问题描述】:

我有一个带有可移动相机的 3D 场景。我有该相机的 3D 坐标(x、y、z -> Y 是高度)以及 X 和 Y 旋转(上/下、左/右)。

我想得到我正在看的地板的坐标 (x1, z1)。

基本上,如果相机在 (0, 4096, 0)(4096 是高度)并且我的 xRotation 是 45º 并且我的 yRotation 是 0,我将看着地板上的点 (4096, 0, 0)

我试图对其进行编程,但我被三角函数卡住了。帮帮我吧。

以下代码是我现在所拥有的,但不能完全正常工作:

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;

【问题讨论】:

    标签: opengl 3d trigonometry perspectivecamera


    【解决方案1】:

    不要为这种三角函数烦恼,使用矩阵和转换来做这种事情通常更实用和易于理解。我建议尝试这种称为 Ray Casting 的方法:

    1. 计算相机视线的方向,你必须得到看 相机方向
    2. 使用光线投射来确定视线的交点和 场景网格(或只是表示地板的平面)。

    现在关于第一个,这里是伪代码:

    XRotMat = CreateRotationMatrixAroundXAxis(verticalCameraAngel);
    YRotMat = CreateRotationMatrixAroundYAxis(horizentalCameraAngel);
    
    CameraSightDir = YRotMat*XRotMa*initialCameraDir;
    

    关于第二步:

    SightRay.Source = Camera.Position;
    SightRay.Direction = CameraSightDir;
    
    Intersection = IntersectRayWithPlane(SightRay , FloorPlane);
    

    IntersectRayWithPlane 是一个相当简单的程序,你可以阅读它here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多