As the title suggests, i'm trying to find a way to extrapolate the pitch, yaw, and roll between two 3D points.
I know how to do this in 2D (atan2 etc), but i'm striking out trying to find a way to do this at all in 3d, let alone in an efficient way.
Any help's appreciated.

**0**

# Angle between two 3D points

Started by tKircher, Sep 27 2008 12:14 PM

7 replies to this topic

Sponsor:

###
#3
Members - Reputation: **175**

Posted 27 September 2008 - 12:54 PM

a dot b = |a||b|cos(theta), where theta is the angle between the two vectors.

So, to compute theta we need (forgive the bad ascii art):

a dot b is just the 3d dot product, nice and efficient :). |a| is the magnitude of the vector a.

So, to compute theta we need (forgive the bad ascii art):

( a dot b )

theta = acos(---------)

( |a||b| )

a dot b is just the 3d dot product, nice and efficient :). |a| is the magnitude of the vector a.

###
#4
Members - Reputation: **122**

Posted 29 September 2008 - 04:06 PM

Ok well, my routine isn't working, and since all the other calculations are taken line-for-line from code that everyone reports as working, i assume it's this.

All i'm doing is taking the dot product of the two points (taking the x, y, z of each point as vectors and dotting those), then using sin, cos, and tan on the dot to get the three values i'm after. I figured an implicit magnitude of 1 would simplify the equation (divide by 1 is sorta redundant)

However, it doesn't seem to be working. The angles aren't correct, it -always- returns 90 degrees when using the arc-tan/cos/sin, and when using normal cos/tan/sin, it constantly returns a 57.29 degree. This isn't correct, as the angle between the two points is, in fact, exactly 180 degrees.

Well, how about this. I have two points. I want to know the angles that point from one point to the other one. Or, another way of thinking, if i have an entity at one point who isn't rotated at all, how many degrees would such an entity have to rotate to be facing -exactly- at that second point.

All i'm doing is taking the dot product of the two points (taking the x, y, z of each point as vectors and dotting those), then using sin, cos, and tan on the dot to get the three values i'm after. I figured an implicit magnitude of 1 would simplify the equation (divide by 1 is sorta redundant)

However, it doesn't seem to be working. The angles aren't correct, it -always- returns 90 degrees when using the arc-tan/cos/sin, and when using normal cos/tan/sin, it constantly returns a 57.29 degree. This isn't correct, as the angle between the two points is, in fact, exactly 180 degrees.

Quote:

Computing the pitch, yaw, and roll 'between two 3D points' doesn't really make any sense. Could you perhaps clarify what it is you're trying to do?

Well, how about this. I have two points. I want to know the angles that point from one point to the other one. Or, another way of thinking, if i have an entity at one point who isn't rotated at all, how many degrees would such an entity have to rotate to be facing -exactly- at that second point.

###
#5
Members - Reputation: **2038**

Posted 29 September 2008 - 04:39 PM

Quote:Sounds like you might be missing a degrees/radians conversion somewhere. (That's just a guess though - maybe you could post the code where you perform these calculations.)

Original post by tKircher

Ok well, my routine isn't working, and since all the other calculations are taken line-for-line from code that everyone reports as working, i assume it's this.

All i'm doing is taking the dot product of the two points (taking the x, y, z of each point as vectors and dotting those), then using sin, cos, and tan on the dot to get the three values i'm after. I figured an implicit magnitude of 1 would simplify the equation (divide by 1 is sorta redundant)

However, it doesn't seem to be working. The angles aren't correct, it -always- returns 90 degrees when using the arc-tan/cos/sin, and when using normal cos/tan/sin, it constantly returns a 57.29 degree. This isn't correct, as the angle between the two points is, in fact, exactly 180 degrees.

Quote:Ok, to rotate an entity to aim at a target, you only need to compute two angles. What you're looking for is a cartesian-to-spherical coordinate conversion, which, given the target position, will return two angles (pitch and yaw, azimuth and elevation - whatever you want to call them).

Well, how about this. I have two points. I want to know the angles that point from one point to the other one. Or, another way of thinking, if i have an entity at one point who isn't rotated at all, how many degrees would such an entity have to rotate to be facing -exactly- at that second point.

Unfortunately it can be kind of tricky to get the conversion right, due to the various differing conventions that are used. Note that there are other options as well that don't involve angles or trig - if this is for billboarding, for example, you can usually construct a suitable orientation directly using only vector math. Finally, if this is for AI, and if you want the entity to turn gradually toward the target rather than snapping to it, it would probably be better to transform the target point into the local space of the entity and then apply pitch and yaw deltas based on the local-space position of the target.

So what, ultimately, are you trying to do? With a little more info, we can probably steer you toward a suitable solution.

###
#6
Members - Reputation: **175**

Posted 29 September 2008 - 08:21 PM

Quote:

Original post by tKircher

All i'm doing is taking the dot product of the two points (taking the x, y, z of each point as vectors and dotting those), then using sin, cos, and tan on the dot to get the three values i'm after. I figured an implicit magnitude of 1 would simplify the equation (divide by 1 is sorta redundant)

Well, there are a few problems here :).

First, the formula does not generalize to plug in sin/cos/tan/asin/atan, it only works with acos. Second, unless you are using normalized vectors, dividing by the magnitude is actually very important.

The most serious problem, is I misread your question (went off the title of the post, not the content ;)). The formula I gave you will certainly compute the angle between two vectors. However, that's all it will do.

jyk's post has some good ideas on how to accomplish this.

###
#7
Members - Reputation: **122**

Posted 29 September 2008 - 08:44 PM

As far as i know u can't calculate an angle between two points ... U can calculate an angle between two vectors (or lines)... I calculate an angle between one static line and a ray pointing from a model that i can drag with mouse...

Here's how i do it:

//calculations for angle between ray and point

//edgingPoints are the edging points of a model

lineHorizontal.X = (edgingPoints[4].X + 0.5f) - edgingPoints[4].X;

lineHorizontal.Y = (edgingPoints[4].Y) - (edgingPoints[4].Y);

lineHorizontal.Z = (edgingPoints[4].Z) - (edgingPoints[4].Z);

lineRay.X = rayEnd.X - intersectionPointOnModel.X;

lineRay.Y = rayEnd.Y - intersectionPointOnModel.Y;

lineRay.Z = rayEnd.Z - intersectionPointOnModel.Z;

double dot1Horizontal = lineHorizontal.X * lineRay.X + lineHorizontal.Z * lineRay.Z;

double dot2Horizontal = lineHorizontal.X * lineHorizontal.X + lineHorizontal.Z * lineHorizontal.Z;

double dot3Horizontal = lineRay.X * lineRay.X + lineRay.Z * lineRay.Z;

double angleCosHorizontal = dot1Horizontal / (Math.Sqrt(dot2Horizontal) * Math.Sqrt(dot3Horizontal));

//conversion from radians to degrees:

horizontalAngle = Math.Acos(angleCosHorizontal) * (180 / Math.PI));

Hope it helps with your problem...

Here's how i do it:

//calculations for angle between ray and point

//edgingPoints are the edging points of a model

lineHorizontal.X = (edgingPoints[4].X + 0.5f) - edgingPoints[4].X;

lineHorizontal.Y = (edgingPoints[4].Y) - (edgingPoints[4].Y);

lineHorizontal.Z = (edgingPoints[4].Z) - (edgingPoints[4].Z);

lineRay.X = rayEnd.X - intersectionPointOnModel.X;

lineRay.Y = rayEnd.Y - intersectionPointOnModel.Y;

lineRay.Z = rayEnd.Z - intersectionPointOnModel.Z;

double dot1Horizontal = lineHorizontal.X * lineRay.X + lineHorizontal.Z * lineRay.Z;

double dot2Horizontal = lineHorizontal.X * lineHorizontal.X + lineHorizontal.Z * lineHorizontal.Z;

double dot3Horizontal = lineRay.X * lineRay.X + lineRay.Z * lineRay.Z;

double angleCosHorizontal = dot1Horizontal / (Math.Sqrt(dot2Horizontal) * Math.Sqrt(dot3Horizontal));

//conversion from radians to degrees:

horizontalAngle = Math.Acos(angleCosHorizontal) * (180 / Math.PI));

Hope it helps with your problem...

###
#8
Members - Reputation: **122**

Posted 30 September 2008 - 01:13 PM

Quote:

First, the formula does not generalize to plug in sin/cos/tan/asin/atan, it only works with acos. Second, unless you are using normalized vectors, dividing by the magnitude is actually very important.

That's all i had to do, as soon as i normalized them the whole thing worked, acos/asin of the dot finally return the correct values!

Thanks for all the help!