Search code examples
3dtrigonometrycoordinate-systemscartesian-coordinates

Converting spherical coordinates to cartesian


I am trying to convert spherical coordinates to cartesian ones to draw a simple 3D pyramid.

Here's the code to get the four main corners of the pyramid, depending on the polar angle yrad and azimuth angle xrad and the summit coords x and y:

xpos1 = x+25*(      sin(yrad+pi/2+pi/4)*cos(-xrad+pi/2+pi/4));
zpos1 = y-35+25*(   cos(yrad+pi/2+pi/4)); 
xpos2 = x+25*(      sin(yrad+pi/2-pi/4)*cos(-xrad+pi/2+pi/4));
zpos2 = y-35+25*(   cos(yrad+pi/2-pi/4)); 
xpos3 = x+25*(      sin(yrad+pi/2-pi/4)*cos(-xrad+pi/2-pi/4));
zpos3 = y-35+25*(   cos(yrad+pi/2-pi/4)); 
xpos4 = x+25*(      sin(yrad+pi/2+pi/4)*cos(-xrad+pi/2-pi/4));
zpos4 = y-35+25*(   cos(yrad+pi/2+pi/4)); 

The azimuth angle axis seems to work fine, but the problem is that when manipulating the polar angle, left and right are swapped as they go over the zenith or bottom, like shown here (select mp4 on the top for smoother playback): http://gyazo.com/4a245713c232893960863cf4ea4186f6

What is wrong?


Solution

  • Hehe, this bug indeed has a nice looking result :) Let's solve it.

    For polar coordinates, we define two angles:

    • Polar Angle (yrad) - angle of rotation around the pole
    • Azimuth Angle (xrad) - angle "above"/"below" the equator line (0 is at the "north pole")

    If we try to visualize it, it would look like so:

          Look from above         |          Look from side           
    (Z goes positive towards you) |    (Y goes negative towards you)  
                                  |                                   
                Y-  Z-            |                Z-  Y+             
                |  /              |                |  /               
                | / "far"         |                | / "far"          
                |/                |                |/                 
       X- ------+-------> X+      |       X- ------+-------> X+       
               /| yrad |          |               /| xrad ^           
       "near" / |<-----+          |       "near" / |------+           
             /  |  "polar angle"  |             /  |  "azimuth angle" 
            Z+  Y+                |            Y-  Z+                 
    

    Using those angles, we can calculate the coordinates in the following way:

             Point := radius * (x = sin(xrad) * cos(yrad)
                                y = sin(xrad) * sin(yrad)
                                z = cos(xrad))
    

    These are the conventional notations (See Spherical coordinate system). When you draw the Cartesian representation of the points, usually you use the X and Y coordinates, however you chose X and Z which is still perfectly OK.

    The mistake you did make however, is that you swapped the angles!

    Your computation  |  x := sin( yrad ) * cos ( xrad )  |  z := cos( yrad )
    ------------------+-----------------------------------+------------------
    Should have been  |  x := sin( xrad ) * cos ( yrad )  |  z := cos( xrad )
    

    So, when you changed the azimuth (xrad), you had only the X coordinates affected and this actually worked out quite well (because cos( xrad ) = sin( 90 - xrad ), so you had a behavior of a sinus (as needed for xrad)). When you rotated the the polar angle (yrad) things began being ugly as the change in both coordinates was not coordinated (no pun intended).