2013-03-22 102 views
3

我有對象A,速度。速度被指定爲3D矢量a = (x, y, z)。位置是3D點A [X, Y, Z]。我需要知道的是,如果當前速度將此對象引向位置B [X, Y, Z]上的另一個對象B.
我在2個維度成功地實現這一點,忽略了第三個:三維矢量角度 - 得到兩個

/*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的速度,我會檢查ID A是否正在向B發送。

+0

你的問題是什麼? 「第二個角度」是什麼意思? – gspr 2013-03-22 22:41:56

+1

我正確地猜測你想知道矢量'a'是否與矢量'B-A'平行?如果是這樣,只需計算他們的[點積](https://en.wikipedia.org/wiki/Dot_product)並查看是否(接近)1. – gspr 2013-03-22 22:44:09

+0

點產品是否會在某個範圍內工作?我的意思是,我能否告訴他們,他們並不平行,但他們很近? – 2013-03-22 23:01:05

回答

4

我假設你要找的「第二個角度」是φ。也就是說,您正在使用球面座標:

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

但是,如果你想要做的是找到如果A與到乙的速度移動,你可以使用點產品的一個基本答案。

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

如果點積爲0,這意味着你沒有得到任何接近 - 你周圍一定半徑|| B-A ||的球體移動以B爲中心。如果點積> 0,則您正在朝着該點移動,並且如果點積爲< 0,則您正在遠離它。

+0

謝謝,這個解釋將在未來多次幫助我,我想:) – 2013-03-22 23:47:42