【问题标题】:get angle between two 3d points获取两个 3d 点之间的角度
【发布时间】:2016-09-01 21:19:16
【问题描述】:

我在 Vector3 类中创建了一个 get_angle 函数,但我遇到了问题。

Y 角度非常好。

它返回的俯仰角 (X) 略高于我的目标位置,当基矢量在它上面时(并且在相反的情况下)。

错误的多少取决于高度差。

Angle get_angle(const Vector3f& v) const { 
    return Angle(
        math::rad_to_deg(atan2(get_distance(v), v.z - z)) - 90.0f, 
        math::rad_to_deg(atan2(v.y - y, v.x - x)), 
        0.0f); 
}

这可能是我的数学不好。

【问题讨论】:

    标签: math angle atan2


    【解决方案1】:

    您到底想计算什么?你的“角度”类代表什么? 我猜你要么想要:

    1. 计算两个向量之间的角度,即单个标量值。公式可以在这里找到cos(theta) == dot(*this, v) / (norm() * v.norm())https://math.stackexchange.com/questions/974178/how-to-calculate-the-angle-between-2-vectors-in-3d-space-given-a-preset-function

    2. 或者,将两个向量都转换为球坐标(phi、theta),并为每个 phi 和 theta 计算一个增量,即计算两个角度。从笛卡尔坐标到球坐标的转换公式可以在这里找到:https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

    【讨论】:

    • 我的 Angle 类代表 3d 空间的 3 个角度:pitch、yaw 和 roll。我解决了我的问题。在另一个帖子中发布了它遇到的问题。
    【解决方案2】:

    我找到了解决问题的方法:

    Angle get_angle(const Vector3f& v) const { 
        return Angle(
        math::rad_to_deg(
            atan2(
            sqrt(pow(X - v.X, 2) + pow(Y - v.Y, 2)), // the problem was here, 
                                                     // the distance between the vectors should not include the Z position distance in it
            v.Z - Z)) - 90.0f,
    
        math::rad_to_deg(atan2(v.Y - Y, v.X - X)), // this worked correctly
    
        0.0f // roll angle should always be 0.0f
        ); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多