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 this post

Link to post

Share on other sites

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 this post

Link to post

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 this post

Link to post

Share on other sites

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?