Well I'm reading the Frank's Luna DirectX10 book and, while I'm trying to understand the first demo, I found something that's not very clear at least for me. In the updateScene method, when I press A, S, W or D, the angles mTheta and mPhi change, but after that, there are three lines of code that I don't understand exactly what they do:

// Convert Spherical to Cartesian coordinates: mPhi measured from +y
// and mTheta measured counterclockwise from -z.
float x = 5.0f*sinf(mPhi)*sinf(mTheta);
float z = -5.0f*sinf(mPhi)*cosf(mTheta);
float y = 5.0f*cosf(mPhi);

I mean, this explains that they do, it says that it converts the spherical coordinates to cartesian coordinates, but, mathematically, why? why the x value is calculated by the product of the sins of both angles? And the z by the product of the sine and cosine? and why the y just uses the cosine? After that, those values (x, y and z) are used to build the view matrix.

The book doesn't explain (mathematically) why those values are calculated like that (and I didn't find anything to help me to understand it at the first Part of the book: "Mathematical prerequisites"), so it would be good if someone could explain me what exactly happen in those code lines or just give me a link that helps me to understand the math part.

Thanks in advance!


1 Answer 1


enter image description here

In the image the red vector is the one we are trying to convert to cartesian, given angles phi & theta (in the description I will refer to the length of the vector as r, for radius of the sphere).

So, the y-coordinate is the easy one, we know what the angle is between the red vector and the y-axis (phi), we just project the vector onto the y-axis;

y=|red| * cos(phi) //|red| means 'length of red vector'
y= r *cos(phi)

[if you don't understand projection, think of it this way, we are trying to find the length of the 'adjacent' side of the yellow triangle = cos(angle)*hypotenuse]

For the other two, we can't project directly, we don't know the angle between the red vector and the z or x-axis. theta is NOT the angle between the vector and the z-axis, it's a measure of how far the vector has been rotated around the y-axis, as measured from the -ive z-axis.

In order to get the x & z coords we first need to project the red vector on the x-z plane, that gives us the length of the blue vector.


[the length of blue is the length of the 'opposite' side of the yellow triangle, it will also be the hypotenuse of the pink triangle]

As can be seen from the diagram we know what angle the blue vector makes with the z-axis (theta) so we can project the blue onto the z-axis;

z= -|blue|*cos(theta)
z=-r*sin(phi)*cos(theta) // minus sign comes from the fact we are projecting onto -ive z-axis

[z is the length of the 'adjacent' side of the pink triangle]

similarly, we can project onto the +ive x-axis

x=|blue|*sin(theta) =r*sin(phi)*sin(theta)

[z is the length of the 'opposite' side of the pink triangle]

  • \$\begingroup\$ Thank you! I have a better idea of what the code does but, I have a question, why the hypotenuse of the pink triangle is the length of the blue vector? I mean, when I saw the image I thought the hypotenuse was the opposite side of the blue vector, but it can't be possible because the value of theta would be always 90º... \$\endgroup\$
    – German
    Nov 30, 2012 at 20:41
  • \$\begingroup\$ I've edited the image to indicate where the right angles are. The pink triangle looks distorted because of they way I am trying draw a 3D scene in 2D. The hypotenuse is always opposite the right angle. \$\endgroup\$
    – Ken
    Nov 30, 2012 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .