Jump to content
  • Advertisement
Sign in to follow this  
Sc4Freak

Angle between 2 points

This topic is 4249 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 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
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.

Share this post


Link to post
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 - A
yaw = math.atan2(V.z, V.x) * 180.0 / math.pi
pitch = math.atan2(V.y, math.sqrt(V.x * V.x + V.z * V.z)) * 180.0 / math.pi
roll = 0

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

Share this post


Link to post
Share on other sites
Quote:
Original post by SantaClaws

yaw = math.atan2(V.z, V.x) * 180.0 / math.pi
pitch = math.atan2(V.y, math.sqrt(V.x * V.x + V.z * V.z)) * 180.0 / math.pi
roll = 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 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
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?

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!