30

So coming from a flash background I have an OK understanding of some simple 2D trig. In 2d with I circle, I know the math to place an item on the edge given an angle and a radius using.

x = cos(a) * r;
y = sin(a) * r;

Now if i have a point in 3d space, i know the radius of my sphere, i know the angle i want to position it around the z axis and the angle i want to position it around, say, the y axis. What is the math to find the x, y and z coordinates in my 3d space (assume that my origin is 0,0,0)? I would think i could borrow the Math from the circle trig but i can't seem to find a solution.

James Hay
  • 12,580
  • 8
  • 44
  • 67

2 Answers2

83

Your position in 3d is given by two angles (+ radius, which in your case is constant)

x = r * cos(s) * sin(t)
y = r * sin(s) * sin(t)
z = r * cos(t)

here, s is the angle around the z-axis, and t is the height angle, measured 'down' from the z-axis.

The picture below shows what the angles represent, s=theta in the range 0 to 2*PI in the xy-plane, and t=phi in the range 0 to PI.

enter image description here

slashmais
  • 7,069
  • 9
  • 54
  • 80
second
  • 28,029
  • 7
  • 75
  • 76
  • 8
    Don't forget that `s` and `t` need to be in radians, *not* degrees. To convert to radians: `radians = angleInDegrees * Math.PI / 180`. – Sam Apr 16 '14 at 17:48
  • If s=45deg and t=0deg, then (x,y,z) = (0,0,1). But when we actually map it, we would get (0, 0.707, 0.707) right? x and y always give zero when t is zero and z is independent of s...! What should I do to get it right? – Saravanabalagi Ramachandran May 02 '17 at 18:34
  • Shouldn't we be able to sweep exactly one circle with t locked down to 0 and varying s? Here, it doesnt work like that, when t is zero, regardless of the value of s, (x,y,z) is always (0,0,1) – Saravanabalagi Ramachandran May 02 '17 at 18:37
  • @SaravanabalagiRamachandran think about where on the sphere you would be when phi equals zero and you'll realize why this is. – David Hoelzer Jun 13 '18 at 21:00
  • It's been a year but here are the answers; _x and y always give zero when t is zero and z is independent of s_ **Nope**; _Shouldn't we be able to sweep exactly one circle with t locked down to 0 and varying s?_ **Yes**, but sweeping a circle requires changes in both y and z – Saravanabalagi Ramachandran Jun 13 '18 at 21:19
  • This formula doesn't seem to work correctly, I found this formula making a research, but not sure how to translate it to pseudocode. https://math.stackexchange.com/questions/1881758/find-point-on-a-sphere-given-two-angles – Enzo Nov 08 '18 at 08:24
  • Hi, trying to reverse engineer this to find the angle s and t for a given point on a sphere but I'm having a hard time. I seem to be getting `nan`s most of the time. Anyone have any idea how to do this? Thanks – Figwig Aug 29 '19 at 16:31
  • for s=45deg, t=45deg, is it correct that x=y but z!=x? – ThorSummoner May 01 '20 at 21:36
  • @second How to calculate the radius when the camera is sitting on the ground 4 meters away from where the object is and The camera is tilted 10 degrees upwards towards the sky? is the `radius =4`? how to convert it to pixels? – S.EB Feb 01 '23 at 11:04
2

The accepted answer did not seem to support negative x values (possibly I did something wrong), but just in case, using notation from ISO convention on coordinate systems defined in this Wikipedia entry, this system of equations should work:

import math

x = radius * sin(theta) * cos(phi)
y = radius * sin(theta) * sin(phi)
z = radius * cos(theta)

radius = math.sqrt(math.pow(x, 2) + math.pow(y, 2) + math.pow(z, 2))

phi = math.atan2(y, x)
theta = math.acos((z / radius))
legel
  • 2,507
  • 3
  • 23
  • 22