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

Thanks for helping

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

Thanks for helping

@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);

var r = this._currentRadius;

var v = new THREE.Vector3( Math.cos(latRad) * Math.cos(lonRad) * r, Math.sin(latRad) * r, Math.cos(latRad) * Math.sin(lonRad) * r );

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 ),
lon: THREE.Math.degToRad( paris.lon )
}
console.log(parisSpherical);
var radius = 10;
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;
let xPos= radiusEarth * Math.cos(latitude_rad) * Math.cos(longitude_rad);
let zPos = radiusEarth * Math.cos(latitude_rad) * Math.sin(longitude_rad);
let yPos = radiusEarth * Math.sin(latitude_rad);
return {x: xPos, y: yPos, z: zPos};
}
```