得到两个3d点之间的角度

时间:2016-09-01 21:19:16

标签: math angle atan2

我在我的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); 
}

这可能是我的数学在这里很糟糕。

2 个答案:

答案 0 :(得分:0)

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

  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的delta,即计算两个角度。从笛卡尔坐标到球坐标的转换公式可以在这里找到:https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

答案 1 :(得分:0)

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

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
    ); 
}