0

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

这可能是我的数学不好。

4

2 回答 2

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

于 2016-09-01T21:40:39.070 回答
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
    ); 
}
于 2016-09-01T21:58:41.663 回答