角度的三维矢量-得到两者

Angles of 3D vector - getting both

本文关键字:三维      更新时间:2023-10-16

我有一个物体A,有一个速度。速度被指定为三维矢量a = (x, y, z)。位置为三维点A [X, Y, Z]。我需要弄清楚,当前的速度是否会把这个物体带到位置B [X, Y, Z]的另一个物体B。
我已经成功地在两个维度中实现了这一点,忽略了第三个维度:

   /*A is projectile, B is static object*/
    //entity is object A
    //  - .v[3] is the speed vector
    //position[3] is array of coordinates of object B    

    double vector[3];                               //This is the vector c = A-B
    this->entityVector(-1, entity.id, vector);      //Fills the correct data
    double distance = vector_size(vector);          //This is distance |AB|
    double speed = vector_size(entity.v);      //This is size of speed vector a
    float dist_angle = (float)atan2(vector[2],vector[0])*(180.0/M_PI);           //Get angle of vector c as seen from Y axis - using X, Z
    float speed_angle = (float)atan2((double)entity.v[2],entity.v[0])*(180.0/M_PI); //Get angle of vector a seen from Y axis - using X, Z
    dist_angle = deg180to360(dist_angle);             //Converts value to 0-360
    speed_angle = deg180to360(speed_angle);           //Converts value to 0-360
    int diff = abs((int)compare_degrees(dist_angle, speed_angle));   //Returns the difference of vectors direction

我需要创建非常相同的比较,使其在3D中工作-现在,Y位置和Y向量坐标被忽略。
我应该做什么计算来得到第二个角度?

根据答案编辑
我用球坐标比较它们的角度来检查两个向量是否指向相同的方向。一个矢量是A- b,另一个矢量是A的速度,我要检查A是否朝着b前进

我假设你正在寻找的"第二个角度"是φ。也就是说,你在使用球坐标:

(x,y,z) => (r,θ,φ)
r = sqrt(x^2 + y^2 + z^2)
θ = cos^-1(z/r)
φ = tan^-1(y/x)

然而,如果你想做的只是找出A是否以速度A向B移动,你可以使用点积作为基本答案。

1st vector: B - A (vector pointing from A to B)
2nd vector: a (velocity)
dot product: a * (B-A)

如果点积为0,这意味着你没有靠近——你在一个半径恒定的球体上运动,而B在圆心。如果点积> 0,就向这个点移动,如果点积<0,你在远离它。