3
votes

I have object A, with a speed. Speed is specified as 3D vector a = (x, y, z). Position is 3D point A [X, Y, Z]. I need to find out, if the current speed leads this object to another object B on position B [X, Y, Z].
I've sucessfully implemented this in 2 dimensions, ignoring the third one:

   /*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

I need to create the very same comparison to make it work in 3D - right now, the Y positions and Y vector coordinates are ignored.
What calculation should I do to get the second angle?

Edit based on answer:
I am using spherical coordinates and comparing their angles to check if two vectors are pointing in the same direction. With one vector being the A-B and another A's speed, I'me checking id A is heading to B.

1
What's your question? What do you mean by "the second angle"?gspr
Am I correct in guessing that you want to know if the vector a is parallel with the vector B-A? If so, just compute their dot product and see if it's (near) 1.gspr
Will dot product work at some range? I mean, will I able to tell, that they are not parallel, but they are close?Tomáš Zato - Reinstate Monica
The dot product of two normalized vectors is the cosine of the angle between them. Close to 1 will mean they're almost parallel, close to -1 will mean they're almost parallel but in opposite directions, close to 0 will mean they're almost orthogonal. Dot products work always.Alexey Frunze
Please ask Math questions at the Mathematics SE.GManNickG

1 Answers

5
votes

I'm assuming the "second angle" you're looking for is φ. That is to say, you're using spherical coordinates:

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

However, if all you want to do is find if A is moving with velocity a towards B, you can use a dot product for a basic answer.

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

If the dot product is 0, it means that you're not getting any closer - you're moving around a sphere of constant radius ||B-A|| with B at the center. If the dot product > 0, you're moving towards the point, and if the dot product < 0, you're moving away from it.