# Angle between 2 points

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

## Recommended Posts

I have 2 points in 3D space. Assuming one is at the origin, how do I calculate the X, Y and Z angle between the origin and the point? With an angle of 0 degrees, an object would point in the positive Z direction (DirectX left-handed).

##### Share on other sites
Quote:
 Original post by Sc4FreakI have 2 points in 3D space. Assuming one is at the origin, how do I calculate the X, Y and Z angle between the origin and the point?With an angle of 0 degrees, an object would point in the positive Z direction (DirectX left-handed).
Well, I'm assuming you want to compute the 'X, Y, and Z angles' for use with D3DXMATRIXYawPitchRoll(), or something to that effect.

The first thing to realize is that you only need two angles to uniquely define a direction, not three.You can compute these angles with a Cartesian-to-spherical coordinate conversion. I'm not sure what conventions you're working with, so I can't tell you specifically how to perform the conversion, or which of the angles can be assigned a value of zero.

Also, perhaps you could describe what you're doing in greater detail. Is this for aiming a gun turret or something similar? Orienting a spaceship? Depending on the context, Euler angles may or may not be the solution you're looking for.

##### Share on other sites
One way:
A = (x: 10.0, y: 10.0, z: 10.0)B = (x: 10.0, y: 15.0, z: 10.0)V = B - Ayaw = math.atan2(V.z, V.x) * 180.0 / math.pipitch = math.atan2(V.y, math.sqrt(V.x * V.x + V.z * V.z)) * 180.0 / math.piroll = 0

... where yaw, pitch and roll are rotations about (0, 1, 0), (1, 0, 0), and (0, 0, 1), respectively.

##### Share on other sites
I believe this is called "Polar Angle".

I think SantaClaws has it right.

##### Share on other sites
Quote:
 Original post by SantaClawsyaw = math.atan2(V.z, V.x) * 180.0 / math.pipitch = math.atan2(V.y, math.sqrt(V.x * V.x + V.z * V.z)) * 180.0 / math.piroll = 0

make sure you check that both arguments to atan2 are nonzero before calling it. One non-zero argument is acceptable, two will get you an error. ;)

##### Share on other sites
<pedantic>
Actually, MSDN states that "atan2 calculates the arctangent of y/x (if x equals 0, atan2 returns π/2 if y is positive, -π/2 if y is negative, or 0 if y is 0.)" for Visual Studio 2005.

But the documentation for Visual Studio 6.0 actually contradicts itself! In one section it states that "if both parameters of atan2 are 0, the function returns 0.", only to say a paragraph later that "atan2 is well defined for every point other than the origin, even if x equals 0 and y does not equal 0." Who knows what to believe.
</pedantic>

##### Share on other sites
Sorry, I didn't catch that -- Python seemed to be returning valid results in my limited tests with 0 for x and y.

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by Sc4FreakI have 2 points in 3D space. Assuming one is at the origin, how do I calculate the X, Y and Z angle between the origin and the point?With an angle of 0 degrees, an object would point in the positive Z direction (DirectX left-handed).
Well, I'm assuming you want to compute the 'X, Y, and Z angles' for use with D3DXMATRIXYawPitchRoll(), or something to that effect.

The first thing to realize is that you only need two angles to uniquely define a direction, not three.You can compute these angles with a Cartesian-to-spherical coordinate conversion. I'm not sure what conventions you're working with, so I can't tell you specifically how to perform the conversion, or which of the angles can be assigned a value of zero.

Also, perhaps you could describe what you're doing in greater detail. Is this for aiming a gun turret or something similar? Orienting a spaceship? Depending on the context, Euler angles may or may not be the solution you're looking for.

This is actually for the orientation of a particle. I want to orient it to the direction of it's movement. I have a vector which stores the direction that the particle is travelling.

SantaClaws's code worked, however I'm now facing some other problems. For example, when the particle is rotated by a certain amount, for some reason the Z rotation (roll) becomes the X rotation again. This issue sounds suspiciously like gimbal lock, so I've decided to move away from Euler angles and on to quaternions instead.

Does anyone have some good resources for using quaternions for representing 3D rotations?

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×