Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

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.

|blue|=r*sin(phi)

[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]

share|improve this answer
    
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º... –  German Nov 30 '12 at 20:41
    
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. –  Ken Nov 30 '12 at 21:07
    
ah, I can see it now, thanks! –  German Nov 30 '12 at 21:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.