Reputation: 12121
I'm using three.js for a small 3D POC project. The following snippet calculates the new x,y,z position for a 3D object in orbit around the origin (0,0,0):
x = rho * Math.cos(theta) * Math.sin(phi);
y = rho * Math.sin(theta) * Math.sin(phi);
z = rho * Math.cos(phi);
In the above example, theta and phi are known and used to calculate the new x,y,z coordinate for the orbiting 3D subject.
The above works well, but I also want to do the inverse.
How do I go about calculating theta and phi, if I only have an x,y,z point, this all in relation to the origin point (0,0,0)?
Upvotes: 1
Views: 2025
Reputation: 66364
IIRC
r = Math.sqrt(x*x + y*y + z*z);
θ = Math.acos(z/r);
φ = Math.atan2(y, x);
x = r * Math.sin(θ) * Math.cos(φ);
y = r * Math.sin(θ) * Math.sin(φ);
z = r * Math.cos(θ);
You seem to have your θ
and φ
swapped the other way around (which is just a notation choice)
Upvotes: 3