# Azimuth, polar and radius from xyz

Hi, how can I calculate azimuth, polar and radius from a given Vector3( x, y, z )?

Thanks for helping

Hi!
Have a look at this:
https://threejs.org/docs/#api/en/math/Spherical.setFromVector3

1 Like

@prisoner849, I didn’t know this utility existed! I spent so much time on this page figuring it out: http://mathworld.wolfram.com/SphericalCoordinates.html

1 Like

@marquizzo
You’re welcome
TBH, I don’t know since which revision it exists in the framework, but for long time already

Hi @prisoner849 & @marquizzo - I did, thanks, but somehow I´m doing it wrong?

F.E. I get in a latitude and longitude (48.8566, 2.3522 → which is Paris).

I´m calculating xyz on a sphere:
var lonRad = -lng * (Math.PI / 180);
var latRad = lat * (Math.PI / 180);

v.x, v.y and v.z are correct then.

If I now want to calculate it vice versa, for instance from these cartesian Vector3 to spherical, I get the wrong values…

I thought it should be some like:
var outRadius = Math.sqrt((v.x * v.x)
+ (v.y * v.y)
+ (v.z * v.z));
var outPolar = Math.atan(outRadius / v.x);
if (v.x < 0) outPolar += Math.PI;
var outElevation = Math.asin(v.y / outRadius);

Latitude starts from the equator, positive to the North, negative to the South.
Spherical coordinate `phi` starts from the positive Y-asix and its maximum value is Math.PI.
Thus to transform one to another:

``````var paris = {
lat: 48.864716,
lon: 2.349014
}
console.log(paris);

var parisSpherical = {
lat: THREE.Math.degToRad( 90 - paris.lat ),
}
console.log(parisSpherical);

var parisVector = new THREE.Vector3().setFromSphericalCoords( radius, parisSpherical.lat, parisSpherical.lon);
// check we did it correctly
var spherical = new THREE.Spherical().setFromVector3(parisVector);
console.log(spherical);
////////////////////////////
``````

2 Likes

Hi I have a doubt why you are rotating the earth this way? earth.geometry.rotateY(-Math.PI * 0.5);

The UVs / texture on the sphere is offset from where it should be for how latitude and longitude should line up on our globe.

If the sphere is not rotated by 90 degrees lat, lon of 0, 0 would be here:

Instead of where it should be:

1 Like

Thank you @prisoner849.

The function works good, but is it possible to make a little tweak, so the of the parameters is the center of the mesh? Currently it works with Vector3(0, 0, 0), but the mesh I work with is not on this coordinates on my scene.

This function helped me to solve my question.

``````var getCoordinatesFromLatLng = function(latitude, longitude, radiusEarth)
{
let latitude_rad = latitude * Math.PI / 180;
let longitude_rad = longitude * Math.PI / 180;