Skip to main content
GameDev.net gamedev.net
🔒 Locked

Spherical to Cartesian conversion

Started by Mariusz Pilipczuk Jan 5, 2013 at 9:18 PM 1 replies 2.2k views
Original Post
Mariusz Pilipczuk
Mariusz Pilipczuk

Hello,

I am writing a 3D first person game, and i came to a problem with converting spherical coordinates to Cartesian coordinates. The problem is with how I use my axis (which i don't want to change because i got used to them). I know that even Wikipedia gives an example of the conversion, however, I have a slight problem with it.

The example on Wikipedia has two angles:

*phi = on the XY plane

*theta = on the ZY plane.

Also, the Wikipedia example assumes the X axis is positive backwards, Y axis is positive to the right, and the Z axis is positive up.

Reference: http://en.wikipedia.org/wiki/Spherical_coordinates

My problem is that my coordinates are as follows:

+X = left

+Y = up

+Z = forward

And I have two angles:

*phi = on the XZ plane

*theta = on the YZ plane.

I know it should be simple to figure out how that conversion goes based on the Wikipedia example, but I simply can't visualize it :(

Does anyone know how to convert that into Cartesian coordinates?

All help is appreciated.

alvaro
alvaro

I think of the angles as longitude and latitude. The height of a point (its Y coordinate) can be computed from the latitude alone, and it's something like sin(latitude). The other two coordinates will be cos(latitude)*cos(longitude) and cos(latitude)*sin(longitude). Figure out which is which, and perhaps you need to flip a sign, depending on your convention. It should be easy to figure out with those hints.

Mariusz Pilipczuk
Mariusz Pilipczuk
I think of the angles as longitude and latitude. The height of a point (its Y coordinate) can be computed from the latitude alone, and it's something like sin(latitude). The other two coordinates will be cos(latitude)*cos(longitude) and cos(latitude)*sin(longitude). Figure out which is which, and perhaps you need to flip a sign, depending on your convention. It should be easy to figure out with those hints.

I did this:

X = -cos(theta)*cos(phi)

Y = sin(theta)

Z = cos(theta)*sin(phi)

...and it worked! (I had some graphics problems, but that's solved now). Thank you very much!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.