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 :slight_smile:
TBH, I don’t know since which revision it exists in the framework, but for long time already :slight_smile:

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… :confused:

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