Angle between 2 points

Started by
6 comments, last by Sc4Freak 17 years, 2 months ago
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).
NextWar: The Quest for Earth available now for Windows Phone 7.
Advertisement
Quote:Original post by Sc4Freak
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).
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.
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.
I believe this is called "Polar Angle".

I think SantaClaws has it right.
Quote:Original post by SantaClaws
yaw = 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. ;)

<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>
Sorry, I didn't catch that -- Python seemed to be returning valid results in my limited tests with 0 for x and y.
Quote:Original post by jyk
Quote:Original post by Sc4Freak
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).
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?
NextWar: The Quest for Earth available now for Windows Phone 7.

This topic is closed to new replies.

Advertisement