Spherical Coordinates



A spherical coordinate system is a coordinate system where the position of a point in 3D space is specified by three numbers:
  • a radial distance r
  • an azimuthal angle θ (theta)
  • a polar angle φ (phi)
Spherical coordinates comes in handy to implement an Arcball Camera.

Imagine to have a virtual ball behind your screen, by clicking with your mouse you pinch the ball and by dragging the mouse you make the ball spin around its center.

We can map the mouse x coordinates to be the θ angle and the mouse y coordinates to be the φ angle.
We also assume that our virtual ball is a unit sphere so the radial distance is always 1.
The problem is that we can specify the position of our camera only using cartesian coordinates so how do we convert spherical coordinates to cartesian coordinates?

Polar Coordinates

Polar coordinates are like spherical coordinates but in 2D. 


We can specify the position of a point using two numbers:
  • a radial distance r
  • an angle
We use polar coordinates to break down the conversion from spherical to cartesian coordinates in two 2D problems.
First we'll compute the position of a point P on the XZ plane using angle θ, then we compute the position of a point P' on the XY plane using angle φ.

Convert spherical coordinates to cartesian coordinates

Computing the position of point P from polar to cartesian coordinates is simple:

Xtheta = cos(θ)
Ytheta = sin(θ)

and here's how we compute the position of point P':

Xphi = cos(φ)
Yphi = sin(φ)

Now how these two points are related?
Look at the following picture:


The circle of angle θ through point P becomes smaller the higher (or lower) it sits in the unit sphere.
P' controls how high or low it sits in the unit sphere.
So in other words we can say that Yphi controls how high or low circle theta sits in the unit sphere while Xphi controls how large its radius is going to be.

Let's say X, Y, Z are the final cartesian coordinates, we compute the position of our camera as follows:

X = Xtheta Xphi r
Y = Yphi r
Z = Ytheta Xphi r

We add r to the equations just in case the radial distance is > 1 or < 1.
Using cos and sin functions the final formula becomes:

X = cos(θ) cos(φ) r
Y = sin(φ) r
Z = sin(θ) cos(φ) r





Comments

Popular posts from this blog

GLSL Shader in Maya Part 1

Unity Shader: Toon Water Shader

Unity Shader: Spherical Mask Dissolve