将 3D 角度/矢量(不是位置)转换为 2 个角度(角度、间距)

时间:2021-04-07 12:23:31

标签: 3d trigonometry angle

我主要在名为 Doom 2 的旧游戏中映射/修改,但在端口中,在编写脚本时我遇到了 3D 世界中的角度问题。 screenshot of model with this problem. Note: Red points are the real and correct normal angles, and the Arrows are my attempt to convert these real 3D angles to doom's angle and pitch but failed and only facing up or down but not right or left... 例如,我这里有某个对象的 MD3/模型/3D 模型,我想显示他的网格的顶点法线以指向它们正对的位置(法线的真正正确角度)。但我只设法(可能)只得到一个俯仰角(或 Z 角),“角度”是指当它向左或向右旋转 360 度时,游戏中也有“滚动”角,但我没有不要认为它很重要。

我试图做的是从真实 3D 角度转换为 Doom 的角度和俯仰角,但我对三角学和它的公式一无所知......我认为这对于知道这些事情的人来说很容易。

注意: 真正的 3D 角度值是从 -1 到 1 浮点数。 doom 的角度从 0 到 360 浮动,俯仰从 -90 到 90 浮动。

这是我转换为厄运的角度和俯仰(上下、左或右角度)的代码:

vector3 ConvertAngletoDoomAngle(vector3 ang) {
    return (
        0,      //angle (this one i have problem with, i want to make arrows turn correctly left and right)
        0,      //roll (this is not important and i bet it's harder to get...)
        90 * ang.z          //pitch (where arrows points down and up was looking kinda correct...)
    );
}

我认为将角度转换为二维角度需要“cos”和“sin”...我不知道:( 目标:这些箭头必须朝向红点。

1 个答案:

答案 0 :(得分:0)

我认为你得到的是表面法向量n,它的分量根据笛卡尔坐标系xyz描述了向量的方向。 >

fig1

并且你想找到关于xz平面的方位角φ,以及xz上方的ψ倾角em> 飞机。

这些首先被描述为绕 y 轴的旋转,然后是绕旋转的 z' 轴的旋转

fig2

这个序列导致

nx = cos(φ)
ny = sin(φ)*sin(ψ)
nz =-sin(φ)*cos(ψ)

逆过程求角度的方法如下

φ = atan2(-nz, sqrt(nx^2+ny^2))
ψ = atan(ny/nx)

其中atan2(dy,dx)是全象限反正切函数,atan(r)是标准反正切函数。

相关问题