PDA

Click to See Complete Forum and Search --> : Vector question


cyborg
Feb 11th, 2006, 09:13 PM
How do I convert a X,Y,Z coordinate to a vector with angles and length?

zaza
Feb 12th, 2006, 07:15 AM
Greetings,

Still doing vectors, eh?

A first point, just to be clear, is that a vector is basically a line. It has a direction and a length. A point is thus distinct and a given point cannot define a vector. However, two points are sufficient to define a line and hence a vector. In this case, therefore, I'll assume that the vector passes through the origin (0,0,0). Hence a valid vector in (x,y,z) is simply that which you specified, your given coordinates (X,Y,Z). Incidentally, the typical notation here is to use lower-case letters to denote the axes and upper-case for a point.
So, to reiterate, a vector describing any given 3D point with respect to the origin is simply (X,Y,Z). From the sounds of things, though, you want to convert between a vector expressed in cartesian (x,y,z) coordinates and one in spherical polar coordinates (r,theta,phi). Spherical polars, as you might guess, interpret 3D space by representing points on the surface of a sphere. In order to do this, you need three pieces of information.

1) The radius of the sphere, r, on the surface of which the point is located.
2) The "angle around" to move, theta. In other words, if you were sat at the origin looking along the x-axis (or any other defined starting axis, as long as you're consistent), how far around anticlockwise would you have to turn before you are looking along the line of your desired point.
3) The "angle up" to move, phi. i.e. when you're looking in the right direction theta, how far up or down do you have to look.

So, in the accompanying diagram, r is represented by the red line joining the origin to the point P, theta and phi are the two angles. X, Y and Z are your coordinates.

The length of the vector r is given by Pythagoras' Theorem:

X^2 + Y^2 +Z^2 = r^2

The two angles come from trig functions:

tan theta = Y/X

The length of the diagonal is sqrt(X^2 + Y^2) and so

tan phi = Z/sqrt(X^2+Y^2)


Don't forget to make sure, if you're coding these math functions, to be sure whether you're measuring the angles in degrees or radians!


zaza

cyborg
Feb 12th, 2006, 02:25 PM
Thanks alot for that answer. It makes perfect sence now.
I guess I'll have to make sure to check if X = 0 and if sqrt(X^2+Y^2) = 0

Another thing. How do I convert back from spherical polar coords?
The math I've tried for that is:
x=r*cos(phi)*cos(theta)
y=r*cos(phi)*sin(theta)
z=r*sin(phi)
But as far as I can see, it doesn't give the correct values..
Let's say that phi = 270 degrees, then it will return the same values whatever value I set theta to.

cyborg
Feb 12th, 2006, 05:16 PM
Ah! I started up a first-person-shooter and started moving around the mouse a bit, and finally figured it out!
If I look straight down (phi=-90 or 270) and move the mouse sideways, it just rotates around itself! It's not rotating around the origin as I thought it would.

Now how do I accomplish that?

zaza
Feb 13th, 2006, 03:08 PM
Hi,

But as far as I can see, it doesn't give the correct values..
Let's say that phi = 270 degrees, then it will return the same values whatever value I set theta to.

Considering this, where will the projection of the point onto the xy plane be when 180 > phi > 90 degrees? It will be in the -x, -y quarter. This actually equates to a different value of theta, if you think about it. The angle of elevation should only be between +90 and -90 degrees; rather than standing where you are and looking further and further up until craning over backwards to see that star in the night sky, you'd turn around until it is directly in front of you and then look up as far up as you need. When phi is +90 or -90, then the value of x is by definition zero, so whatever direction you're looking in, you can't change the value of x. You're looking directly upwards or directly downwards, and no matter which way you turn you're still looking at the same spot. This is exactly as expected if X=0. This behaviour will follow naturally if you constrain +90>= phi >= -90 and work on the basis of aligning to theta. This is what I meant in point 3 in the previous post:

3) The "angle up" to move, phi. i.e. when you're looking in the right direction theta, how far up or down do you have to look.

BTW, the equations you've given above to transform back are correct.

zaza