Sign in to follow this  

Vector to Polar?

This topic is 2817 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

Hey Guys, I have a unit vector describing a directional light's direction and i want to convert it to polar to be shown in my editor as a pitch and yaw value, then when those values are changed, convert it back into a vector and store it as the light's direction. My math is doing all sorts of crazy stuff and is just not right at all (like if i put in 1.57 radians (90 degrees), it converts it to a vector, then converts it back to polar to update the editor display and it's showing up as 3.14 radians (180 degrees), not 1.57 (90 degrees)!) If it matters, in my X,Y,Z system, Y is the up and down axis. Here's my math, does it look right to you guys? BTW the light direction vector comes in normalized so I don't think it's a normalization issue. Also I removed all of the radians to degrees conversion stuff so am only working in radians and the problems are still happening so i dont think it's a conversion issue. //convert from vector to polar float fYaw = atan2(vLightDir.z,vLightDir.x); float fPitch = acos(vLightDir.y); //convert from polar to vector vLightDir.x = cos(fYaw)*sin(fPitch); vLightDir.z = sin(fYaw)*sin(fPitch); vLightDir.y = cos(fPitch); Thanks!

Share this post


Link to post
Share on other sites
Thanks, yeah i do mean spherical coordinates thanks for the terminology fix.

It might help me scour google better.

But hey check out how the math works, let's say we had a yaw of 90 degrees and a pitch of 0 degrees and we are trying to convert it to a vector again:

//convert from polar to vector
vLightDir.x = cos(fYaw)*sin(fPitch);
vLightDir.z = sin(fYaw)*sin(fPitch);
vLightDir.y = cos(fPitch);

plugging in the numbers you get (working in degrees for easier reading):

vLightDir.x = cos(90) * sin(0);
vLightDir.z = sin(90) * sin(0);
vLightDir.y = cos(0);

that means...

vLightDir.x = 0 * 0
vLightDir.z = 1 * 0
vLightDir.y = 1

so... the vector out is 0,1,0

which is really 0 yaw and 90 degrees pitch since Y is the up vector.

it seems something is messed up but i'm not sure what

Share this post


Link to post
Share on other sites
The math isn't messed up. The only thing is that you named the angles the one way but uses a convention for interpretation the other way (AFAIS).

I.e. exchange the naming of yaw and pitch

vLightDir.x = cos(fPitch)*sin(fYaw);
vLightDir.z = sin(fPitch)*sin(fYaw);
vLightDir.y = cos(fYaw);

and you get for your example

vLightDir.x = cos(0°)*sin(90°) == 1
vLightDir.z = sin(0°)*sin(90°) == 0
vLightDir.y = cos(90°) == 0

what matches your expectations (presumably).



EDIT: Hmmm. I must think another minute about this. Computing yaw from the xz seems me correct, too.

[Edited by - haegarr on March 28, 2010 1:54:37 PM]

Share this post


Link to post
Share on other sites
Quote:
it seems something is messed up but i'm not sure what
Your code treats a pitch of 0 as straight up, which may be causing you problems. Try this instead:
//convert from vector to polar
float fYaw = atan2(vLightDir.z,vLightDir.x);
float fPitch = asin(vLightDir.y);

//convert from polar to vector
vLightDir.x = cos(fYaw)*cos(fPitch);
vLightDir.z = sin(fYaw)*cos(fPitch);
vLightDir.y = sin(fPitch);
Note also that:

1. To be fully correct, when converting from vector to spherical form, it should be handled as a special case when the vector is parallel or nearly parallel to the up vector.

2. You should clamp the input to asin() to the range [-1, 1] to correct for possible numerical error.

Share this post


Link to post
Share on other sites

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

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