Sign in to follow this  
transwarp

[.net] Vector3 into a yaw and a pitch angle

Recommended Posts

Hello world! ;) I'm wondering how I can convert a normalized DirectX Vector3 into a yaw and a pitch angle, as well as the other way round. Does DirectX offer a function for this or do I have to do the math manually? Greetings, transwarp [Edited by - LessBread on June 11, 2006 4:55:06 PM]

Share this post


Link to post
Share on other sites
I don't know if there's a function to do it, but it's easy enough:
double yaw = Math.Atan2(ds.X, ds.Y);
double pitch = Math.Atan2(ds.Z, Math.Sqrt((ds.X * ds.X) + (ds.Y * ds.Y)));


I can't remember what axis system DX uses, this is right-handed with Z vertical so you might need to switch some axes around.

Share this post


Link to post
Share on other sites
Thanks a lot for the hint. :)
I had already managed the first calculation, but somehow got mixed up with the second.

What really is giving me a headache (due to my limited experience with trigonometry I guess :( ) is the other way round: Creating a normalized vector using given values for yaw and pitch.

Share this post


Link to post
Share on other sites
If you want to go from angles to a vector, you can simply create a matrix from the angles, then multiply the origin vector by the matrix (the origin vector might just be one of the cardinal axes, I use the negative Z axis for that's what OpenGL looks down by default).

Share this post


Link to post
Share on other sites
'Simply creating a matrix from the angles' involves doing exactly the same calculations as I wrote there, and it's less obvious what's going on. It might be faster if it's native on the graphics card, but it's best to do things in a simple way before making optimisations.

For the reverse, it's even easier. Draw a sphere and some lines to work it out, but the answer is (using the same axes as before):

x = cos(yaw)×cos(pitch)
y = sin(yaw)×cos(pitch)
z = sin(pitch)

Share this post


Link to post
Share on other sites
@Bob Janova:

I looked up which axes DX uses: The default seems to be a left-handed with Z going "into the depth".

I just implemented your two calculation blocks in C# and the code seems to work fine:
X = Math.Sin(yaw) * Math.Cos(pitch);
Y = Math.Cos(yaw) * Math.Cos(pitch);
Z = Math.Sin(pitch);
yaw = Math.Atan2(vector.X, vector.Y);
pitch = Math.Atan2(vector.Z, Math.Sqrt((vector.X * vector.X) + (vector.Y * vector.Y)));

I switched your code for X and Y. Was that correct?

How do I have to swap these axes to use a left-handed system as mentioned above?

Thanks a lot for your help.

[Edited by - transwarp on June 12, 2006 4:06:22 PM]

Share this post


Link to post
Share on other sites
It's possible I forgot which axis I was measuring yaw from, definitely! If your code is now reversible then I probably got it wrong.

If the Y axis is vertical, then you want to use my 'Z' and 'pitch' code for DirectX's Y axis, i.e. replace Z with Y. The other axes depend on where your zero-yaw point is, but I'd guess replacing Y with Z (or maybe -Z) would be right.

Share this post


Link to post
Share on other sites
Thank you very much! I have run a number of tests with the code and now everything is working just I had planned.

In case anyone is interested, here's my final code:
double X = Math.Sin(Yaw) * Math.Cos(Pitch);
double Y = Math.Sin(Pitch);
double Z = Math.Cos(Yaw) * Math.Cos(Pitch);

double yaw = Math.Atan2(vector.X, vector.Z);
double pitch = Math.Atan2(vector.Y, Math.Sqrt(vector.X * vector.X + vector.Z * vector.Z));

And here's a little "graphic" ;) to show the used axis':

^........^
|......../
|....../..
Y....Z....
|../......
|/........
.--X-->

[Edited by - transwarp on June 15, 2006 5:13:51 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this