我有一个物体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的速度,我正在检查A是否朝着B前进。
a
是否与向量B-A
平行,我的猜测正确吗?如果是的话,只需计算它们的点积,看看它是否接近于1即可。 - gspr