我有對象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發送。
你的問題是什麼? 「第二個角度」是什麼意思? – gspr 2013-03-22 22:41:56
我正確地猜測你想知道矢量'a'是否與矢量'B-A'平行?如果是這樣,只需計算他們的[點積](https://en.wikipedia.org/wiki/Dot_product)並查看是否(接近)1. – gspr 2013-03-22 22:44:09
點產品是否會在某個範圍內工作?我的意思是,我能否告訴他們,他們並不平行,但他們很近? – 2013-03-22 23:01:05