Converting from spherical coordinates to cartesian around arbitrary vector N - math

So if I'm given an arbitrary unit vector N and another vector V defined in spherical coordinates theta (polar angle between N and V) and phi (azimuthal angle) and r = 1. How do I convert vector V into cartesian coordinates?
Now, I know that in general the conversion from spherical to cartesian is as follows:
x = r * sin theta * cos phi
y = r * sin theta * sin phi
z = r * cos theta
However, since the angles theta and phi are defined respective to the vector N and not the axes, the above conversion wouldn't work, yes? So how would I go about modifying the conversion?

I feel that this is simply not possible given the information you have to hand.
You cannot have a vector V with spherical polar components defined relative to another vector. In a standard spherical polar coordinate system, the coordinates of a point P are given by (r,theta,phi) where theta is the polar angle, phi azimuthal angle, and r the Euclidean distance from the origin. The polar angle is the angle between the z-axis and the line joining the origin to the point P. The azimuthal angle is defined as the angle between the x-axis and the line that joins the origin to the orthogonal projection of P onto the xy plane.
Sometimes the definitions of these two angles are reversed. The above is clearly illustrated at the wiki page http://en.wikipedia.org/wiki/Spherical_polars
The point here is that the angles are defined relative to two mutually orthogonal axes - z and x in this case. Thus you cannot have both of your polar and azimuthal angles defined relative to a single vector N - You can have ONE of them measured relative to N but not both.
As it stands, your problem cannot be solved without providing another vector orthogonal to your N that provides the axis to which the other angle (either polar or azimuthal) is measured.
Your description of N indicates that this is the z-axis of some rotated coordinate system that V takes its polar angle relative to. You need another vector that gives the x-axis of the same rotated coordinate system that V takes measures its azimuthal angle relative to. With that information you can obtain the rotation matrix that maps your rotated coordinate system axes onto the cartesian coordinate axes - from there you will have sufficient information to obtain the cartesian coordinates of V that you require.

Look at this one: http://www.ewerksinc.com/refdocs/coordinate%20and%20unit%20vector.pdf
On page 7 you'll find the conversion formulas between spherical and cartesian vectors.

Related

Convert earth-centric coordinate frame to coordinate frame aligned to tangential plane?

Given that earth is perfectly spherical with radius R.
The earth-centric coordinate system E is defined as follows:
The center of this sphere is the origin,
Earth's north pole represents the z-axis.
Latitude 0 and longitude 0 represent x-axis.
Latitude 0 and longitude 90 represent y- axis.
Now at any given latitude, longitude, and altitude, we can make a local coordinate system S whose y-z plane is tangential to earth's surface and z points to the north pole and x points perpendicular to this plane.
I need a 4x4 transformation matrix to transform a 3d point from earth-centric coordinate system E to this local coordinate system S.
Transformation matrix from S to E might be composed as product of matrices:
Shift along X axis by R+Altitude
Rotation about Y-axis by Latitude
Rotation about Z-axis by Longitude
Make inverse of this matrix to get E-S transform
Assuming that earth is spherical, this is actually not that hard.
Spherical coordinates to the rescue (see here)! A sphere can be parametrized by 2 angles (as already mentions in the problem statement). Based on this, you can formulate equations to convert to cartesian coordinates. If you compute the derivative of those equations with respect to both angles, you get equations stating the tangent and bitangent of any point on the sphere. Based on this you can either use the vector pointing from the center to a point on the sphere as the normal or the cross product between tangent and bitangent. Formulations for tangent and bitangents are also given in the link above.
Now you got an orthogonal system for each point on the sphere based on your 3 vectors: tangent, bitangent and normal. The only part that is missing is the translation which is simply the vector pointing from the center to a point on the sphere. Given all the necessary ingredients, you can create a 4x4 matrix from those axes using standard libraries like glm or simply place those vectors as columns of your matrix (don't forget to normalize tangent, bitangent and normal!). Depending if you use row-major or column-major matrices you may need to transpose this matrix.

how to get a vector from an angle

I'm making a game in pygame and I want a spaceship to move in the direction it's facing. I have the angle and the magnitude, how can I get the direction in the form of a normalized vector that I can then add to the spaceship's x and y to make it move?
What you want to do is change polar coordinates to Cartesian coordinates.
So as I see it you are in 2D. Now you can not avoid using trigonometry.
Let's assume that you are facing with an α angle beginning from the x axis, and growing towards y. Let d be the magnitude.
Vx = cos(α) * d
Vy = sin(α) * d
Now this is not normalized, as I have already multiplied it with d, the magnitude of the speed.
Please check what type of angle your sin and cos functions take, and give matching inputs (it can be radians or degrees). An easy way to check this is to print out sin(30). If it is 0.5, you are in degrees. If it is -0.988... you are in radians.

polar coordinates of a vector in three-dimensional space

polar coordinates of a 2 dimensional vector are:
x = r cos θ
y = r sin θ
What will be the polar coordinates of a vector in 3D (x, y, z)?
From Wikipedia:
x = r sin θ cos φ
y = r sin θ sin φ
z = r cos θ
It depends on what coordinate system you want in 3D. The above 2D transformation can be extended to both spherical and cylindrical coordinates via two clear geometrical analogues. For the case of cylindrical coordinates you would keep the above transformation for both x and y, but for z, the transformation would be given simply by z = z. So the transformation would be
(x, y, z) -> (r, theta, z)
For spherical coordinates there is an introduction of an additional coordinate transformation in the z-direction (see Ignacio Vazque-Abrams answer above) and also changes to the x and y transforms. In this case you have
(x, y, z) -> (r, theta, phi)
I think in your case you would be best using cylindrical coordinates. I hope this helps.
You can use the spherical coordinate system:
http://en.wikipedia.org/wiki/Spherical_coordinate_system
The link shows the conversions to x,y,z.
A goo definition for 3-dimensional polar coordinates appears in this link.
https://www.bing.com/videos/search?q=3+dimensional+polar+coordinates&&view=detail&mid=89E852F2356FEC20286E89E852F2356FEC20286E&&FORM=VRDGAR&ru=%2Fvideos%2Fsearch%3Fq%3D3%2520dimensional%2520polar%2520coordinates%26qs%3Dn%26form%3DQBVDMH%26%3D%2525eManage%2520
If you try to use the Mathematica function CoordinateTransform to conert a 3D cartesian point into a polar point, a message will appear saying it can't be done.
If you try to use the Mathematica function ToPolarCoordinates, an incorrect answer will appear with no indication that the result is wrong.
I can't figure out why there is so little literature on 3D polar coordinates or why Mathematica has not supported it correctly by now.
By the way, 3D polar coordinates are not the same as spherical or cylindrical coordinates.

Given start point, angles in each rotational axis and a direction, calculate end point

I have a start point in 3D coordinates, e.g. (0,0,0).
I have the direction I am pointing, represented by three angles - one for each angle of rotation (rotation in X, rotation in Y, rotation in Z) (for the sake of the example let's assume I'm one of those old logo turtles with a pen) and the distance I will travel in the direction I am pointing.
How would I go about calculating the end point coordinates?
I know for a 2D system it would be simple:
new_x = old_x + cos(angle) * distance
new_y = old_y + sin(angle) * distance
but I can't work out how to apply this to 3 dimensions
I suppose another way of thinking about this would be trying to find a point on the surface of a sphere, knowing the direction you're pointing and the sphere's radius.
First of all, for positioning a point in 3D you only need two angles (just like you only needed one in 2D)
Secondly, for various reasons (slow cos&sin, gimbal lock, ...) you might want to store the direction as a vector in the first place and avoid angles alltogether.
Anyway, Assuming direction is initially z aligned, then rotated around x axis followed by rotation around y axis.
x=x0 + distance * cos (angleZ) * sin (angleY)
Y=y0 + distance * sin (Anglez)
Z=z0 + distance * cos (angleZ) * cos (angleY)
Based in the three angles you have to construct the 3x3 rotation matrix. Then each column of the matrix represents the local x, y and z directions. If you have a local direction you want to move by, then multiply the 3x3 rotation with the direction vector to get the result in global coordinates.
I made a little intro to 3D coordinate transformations that I think will answer your question.
3D Coordinates
First, it is strange to have three angles to represent the direction -- two would be enough. Second, the result depends on the order in which you turn about the respective axes. Rotations about different axes do not commute.
Possibly you are simply looking for the conversion between spherical and Cartesian coordinates.

Calculate 3d Vector perpendicular to plane described by a Point and True North Heading

I have a Point on the surface of the earth which I am converting to a Vector from Earth Center.
I have a True North Heading in degrees describing the path the point will travel on the surface of the earth.
I need to calculate a Vector which is perpendicular to the plane created by the path of this point along the earths surface.
I have tried calculating an arbitrary point along the path using the method described here
and then taking the cross product of the two vectors however it does not seem to be quite accurate enough and seems like more overhead than is necessary.
This is related to my other post ray-polygon-intersection-point-on-the-surface-of-a-sphere.
I'm assuming you're trying to compute a vector lying in the plane of the path, not perpendicular to it (since you've already got one - namely the vector from the origin to your point).
You first need to compute vectors lying in that plane that point due north and due east. To do this, let's call P your point, O the origin, and N = (0, 0, R) is the point at the top of your sphere. Then
e = cross(N - P, P - O)
is a vector that points due east, and is tangent to the sphere because it's perpendicular to P - O, a radius of the sphere.
For similar reasons
n = cross(e, P - O)
will point due north, and will be tangent to the sphere.
Now normalize n and e, and you've got an orthonormal basis for the tangent space at your point. To find a vector in a direction theta (say, counterclockwise from the positive east axis, to simplify the math), just take a little of e and a little of n:
v = cos(theta) * e + sin(theta) * n
Here's my understanding of your problem:
You have a point on the Earth's surface, specified as latitude/longitude coordinates
The direction "true north" is the direction that a person at that point would travel to reach the (geographic) North Pole by the most direct possible route. That is, the "true north vector" is tangent to the Earth's surface at your chosen point and points directly north, parallel to a line of longitude.
The direction of the point's motion will be (initially) tangent to the Earth's surface at your chosen point.
You have an angle in degrees from true north which specifies the heading at which this point is going to move.
This angle is the angle between the "true north vector" and the direction of motion of the point.
You want to calculate a vector that is tangent to the Earth's surface at that point but perpendicular to the direction of motion of the point.
If I've understood all that correctly, you can do it as follows:
The "true north vector" at latitude lat, longitude lng is given by [-sin(lat) * cos(lng), -sin(lat) * sin(lng), cos(lat)]
A vector perpendicular to the "true north vector" which points along a line of latitude (to the east) is given by [-sin(lng), cos(lng), 0]
Since these two vectors identify the plane tangent to the Earth's surface, and the vector specifying the direction of motion of your point is also in that plane, your motion vector is a linear combination of the previous two: [
-(sin(lat) * cos(lng) * cos(th) + sin(lng) * sin(th))
-(sin(lat) * sin(lng) * cos(th) - cos(lng) * sin(th))
cos(lat) * cos(th)
] where th is your heading angle.
To find a vector perpendicular to that motion vector, you can just take the cross product of the radius vector (that is, the vector pointing from the center of the Earth to your point,[cos(lat) * cos(lng), cos(lat) * sin(lng), sin(lat)] with the motion vector. (That math would be messy, best to let the computer handle it)
You already have 2 vectors:
N = (0,0,1) points straight up from the origin.
P = (a,b,c) points from the origin to your point.
Calculate the unit vector to your point
U = P/|P|
Calculate a unit vector perpendicular to U and N
E = U X N
Calculate a unit vector perpendicular to U and E (this will be tangent to the sphere)
T = U X E
T could be pointing either North or South, so
if T.z < 0, multiply T by -1.
T now points due north, and is parallel to the plane tangent to the sphere at P.
You now have enough information to construct a rotation matrix (R), so you can rotate T around U. You can find how to make a matrix for rotation around any axis on wikipedia:
Using R, you can calculate a vector pointing in the direction of travel.
A = RT
A is the answer you are looking for.

Resources