Angle Between Two 3D Points

Started by
16 comments, last by Fenrisulvur 14 years, 7 months ago
I'm trying to find the angle between two 3D points, using the code below. However, the angle between 0,0,0 and 5,5,5 comes out to be 90deg while the angle between 0,0,0 and 5,0,0 comes out to be 45deg. Seems like the results are flipped around so I don't know if I'm doing this right or not. Are there problems with the function? float FindAngle(Data* pointA, Data* pointB) { double pi = 3.14159265358979323846; float angle = 0; float deltaY = pointB->y * pointA->y; float deltaX = pointB->x * pointA->x; float deltaZ = pointB->z * pointA->z; //Calculate the angle angle = (acos(deltaY+deltaX+deltaZ)*(180/pi)); return angle; }
Advertisement
You need to divide by the vector lengths, unless you have normalized vectors.
float lenA = sqrt(pointA->x*pointA->x + pointA->y*....);float lenB = ..;delta = ..float value = (deltaX + ..) / (lenA * lenB);angle = acos(value)...;
The thing is that I don't have vectors,I only have points.
Same thing, use that method, it works.
I did ask you said, but for the result of angle, I keep getting -1.#IND000 and I'm not sure what that means.

Heres the new function:

float FindAngle(Data* pointA, Data* pointB)
{

double pi = 3.14159265358979323846;

float lenA = sqrt(pointA->x*pointA->x + pointA->y*pointA->y + pointA->z*pointA->z);
float lenB = sqrt(pointB->x*pointB->x + pointB->y*pointB->y + pointB->z*pointB->z);

float deltaY = pointB->y * pointA->y;
float deltaX = pointB->x * pointA->x;
float deltaZ = pointB->z * pointA->z;

float value = ((deltaY + deltaX + deltaZ)/(lenA * lenB));
float angle = (acos(value)*(180/pi));

return angle;
}
What angle exactly do you want to find?
When you have one point at 0,0,0 there is no angle to the other point.. there's just a line. You get that value (infinity) because of division by zero, since the length of (0,0,0) is 0. Perhaps you want to find the angle from the xz-plane to the line between the two points or something?
Well in runtime its going to be variable, but for now I'm trying to find the angle between 0,0,0 and 5,5,5.
There is no angle between 0,0,0 and 5,5,5. Those are two points, and there's a line between them. Draw a picture of the angle you want. =)
Sorry, that was an incorrect explanation on my part.

Imagine a person facing the north. This function is meant to calculate how much they would have to turn to face east in degrees. I want to do the same basic thing in 3D.
Then North would look something like (x,0,0), and East would look something like (0,0,-x), where x is however large a number you want (1 if you're dealing with unit vectors).

EDIT: I'm taking vectors as (x,y,z) where y is vertical - nfi if that's correct here, I don't do 3D. >_>

This topic is closed to new replies.

Advertisement