# Are Spherical coordinates flipped?

Q: Are Spherical coordinates theta and phi flipped in Vector3 source code?

Im walking through class Spherical
https://threejs.org/docs/?q=spher#api/en/math/Spherical
And getting the results back to a position vector from method setFromSpherical of which is setFromSphericalCoords

In my mini test in testing to get back Cartesian coords

And according to the wikipedia article and threejs’s notes
Theta Should be Up to Down
And Phi should be around

Now I know its in Z up axis. But I would assume from reading the source it’s converting it properly.

But when just sliding Theta I would expect up down, instead its reverse Phi is up down, Theta is around.

And when I dig through other papers is found this

Convert from spherical coordinates to rectangular coordinates

These equations are used to convert from spherical coordinates to rectangular coordinates.

• 𝑥 = ρ sin φ cosθ
• 𝑦 = ρ sin φ sinθ
• 𝑧 = ρ cos φ

compared to THREEs

``````const sinPhiRadius = Math.sin( phi ) * radius;
this.x = sinPhiRadius * Math.sin( theta );
this.y = Math.cos( phi ) * radius;
this.z = sinPhiRadius * Math.cos( theta );
``````

Maybe im just ignorant in this space still
It does not help that all discussions of this coordinate system talk of different orientations in each persons example instead of a unified mode. right hand left hand kdjfni3roiwjeff

2 Likes

You may be right? But also fixing it could break the entire thrinternet. Might be worth noting in the docs or something tho…

also like… greek letters for roll/pitch/yaw has always bugged me.

whoops oh!! hmmm not sure if i should try a bug post

In your link, z is the vertical axis. In 3js, y is the vertical axis. In my opinion this is the natural convention since every xy diagram uses y as the vertical axis. If z is added there is only room for this in the plain. Unless you exchange it for y. z is the depth value. In addition I prefer the spherical coordinates with the equator as reference and theta with -90 to +90 degrees instead of from the north pole 0 to south pole 180 degrees. But that is purely subjective.
On wiki the representation like in your link is described as inconsistent with the polar coordinates.

It depends heavily on the intended use. I also like to swap x and z because the camera is oriented in minus z by default. That looks like this, for example:

``````function SphericalToCartesian(r, theta, phi){

const x = r * Math.cos(theta) * Math.sin(phi);
const y = r * Math.sin(theta);
const z = r * Math.cos(theta) * Math.cos(phi);

return new THREE.Vector3(x, y, z);
}

//						theta = 90
//                    		   +y     -z  phi = +/- 180
//					            |    /
//					            |  /
//	      phi = -90	-x	________|/________ +x   phi = +90
//						       /|
//					  		 /  |
//					  	   /	|
//	             phi = 0 +z    -y
//     						 theta = -90

``````

As a physicist, I am very mathematically inclined and I wonder why people like to swap y with z as soon as it becomes three-dimensional. I personally find this confusing. I think it’s logical that the z axis is in the plane.
If you want theta and phi to be swapped, then suggestions would be the right topic.

But z and y will certainly not be swapped and I think that’s a good thing because in my opinion it’s good and natural as it is.

1 Like

Here’s a reason why I flipped the Y and Z axis: my game is a top-down game where the world is rendered in front of the camera rather than underneath it. This allows the world to just use X and Y coordinates (which I find more intuitive) and eliminates gimbal lock.

1 Like

Then all I would suggest is to reword the line

The poles (phi) are at the positive and negative y axis. The equator (theta) starts at positive z.

on the docs page three.js docs
So it’s more clear of the developer decision to flip.

Cause to me it feels like its being redundant for a good reason when done looking at the article.

That said, THREE is no stranger to breaking things to fix things in a next version and just giving a slight warning in the changelog

Yup… it’s a debate with 3 choices, but we all agree that X up is an abomination.

1 Like

I do not agree

X-up is perfectly normal to me. At least it is as normal as Y-up and Z-up. That’s why Suica can be set into any orientation or handedness:

https://boytchev.github.io/suica/examples/suica-orientation.html

As for spherical coordinates, their conversion into Cartesian can be done with different sets of equations – it depends on the direction of axis and on angles (where is their origin and direction). So, for me the coordinates are not flipped – it is just that there are several possible sets.

1 Like

Ok I’m switching to X up.

You are cruel. If you start using X-up, I will lose my uniqueness…

4 Likes

Hmm. ok… I’ll try W up.

5 Likes

Thats funny

2 Likes
3 Likes

You can tell it’s Friday

3 Likes

This is hurting my brain.

1 Like

It has 4 dimensions with the quaternions
Quaternion - Axis, Angle Visualization

Incidentally, the 3-dimensional space is only a special case of an n-dimensional vector space.

And then there is the curvature of space in the universe and things like that …

1 Like