Jump to content
  • Advertisement
Sign in to follow this  
Dryak

Angle Between Two 3D Points

This topic is 3264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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)...;

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. =)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. >_>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!