Skip to main content
gamedev.net
🔒 Locked

Vector - Angles

Started by transwarp Aug 23, 2006 at 1:40 PM 5 replies 1.1k views
Original Post
transwarp
transwarp
Hello world, I'm currently developing my first 3D-game using DirectX. Unfortunately I have stumbled upon a seemingly simple mathematical problem that I have not been able to solve properly so far. Note: This is a left-handed coordinate system (X to the right, Y upwards and Z "into" the monitor) I have a 3D-Vector with its X, Y and Z components. Now I would like to calculate the horizontal and vertical angles of the resulting “arrow”. In other words I need to determine the yaw and pitch of a vector. Additionally I need to reverse this operation: Take two angles and a length and build a 3D-Vector from it. This is what I have done so far: Vector to Angles: Yaw = Atan2(Z, X) Pitch = Atan2(Z, Sqrt(X² + Y²)) Length = Sqrt(X² + Y² + Z²) Angles to Vector: X = Cos(Yaw) * Cos(Pitch) * Length Y = Sin(Pitch) * Length Z = Sin(Yaw) * Cos(Pitch) * Length Sqrt = square root Atan = Inverse tangent At first these calculations seemed to be correct. However, after implementing them in the game, I noticed that the Vector to Angles calculation wasn’t giving me the expected results. Can anybody tell me what I have done wrong and how to fix it? Regards, Bastian [Edited by - transwarp on August 23, 2006 4:16:33 PM]
Zakwayda
Zakwayda
What language are you using? If you have access to an 'atan2' function, re-arranging your vector-to-angles calculations to take advantage of it would likely clear up the problems you're encountering.
transwarp
transwarp
I am using C# so I do have an Atan2 function. I edited my post accordingly.
However I do not really see how this makes the solution easier. This is in fact how my code currently looks like.
SuperNerd
SuperNerd
Here is how I would do it

yaw = atan2(x, z)
pitch = atan(y / sqrt(x2 plus z2))
length = Sqrt(x2 plus y2 plus z2)

z = cos(yaw) * cos(pitch) * length
x = sin(yaw) * cos(pitch) * length
y = sin(pitch) * length

I think that should work.
SuperNerd
SuperNerd
Quote:
Original post by transwarp
I am using C# so I do have an Atan2 function. I edited my post accordingly. However I do not really see how this makes the solution easier.


The range of atan is (-π/2, π/2) half a circle.
The range of atan2 is (-π, π) a full circle.

Using atan will disallow the point from going behind origin.

Zakwayda
Zakwayda
As noted, atan2() is not only more robust than atan(), but has fundamentally different behavior.

Anyway, I think you want:
Pitch = Atan2(Y, Sqrt(X² + Z²))
Rather than:
Pitch = Atan2(Z, Sqrt(X² + Y²))
transwarp
transwarp
Quote:
Original post by SuperNerd
The range of atan is (-π/2, π/2) half a circle.
The range of atan2 is (-π, π) a full circle.
Using atan will disallow the point from going behind origin.

Thanks a lot for clarifying that. :)


Quote:
Original post by SuperNerd
yaw = atan2(x, z)
pitch = atan(y / sqrt(x2 plus z2))

Quote:
Original post by jyk
Pitch = Atan2(Y, Sqrt(X² + Z²))

Thanks a lot, both of you. Now the calculation works just perfect. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.