# Rotating co-ordinates in 3D

Suppose I have 3 axes, $x$, $y$, and $z$ such that $x$ is horizontal, $y$ is vertical, and $z$ goes in/out of the computer screen where $+$ve values stick out and $-$ve values are sunken in.

Suppose I have a spherical co-ordinate system where $r$ is the radius from the origin $(x, y, z) = (0, 0, 0)$, $\theta$ is the rotation about the $x$ axis and $\phi$ is the rotation about the $y$ axis, such that $(r,\theta,\phi)=(1,0,0) \mapsto (x, y, z) = (0, 0, 1)$. Assume that I rotate about $y$ first, then about $x$.

I have a number of points, each of which has an associated $r, \theta, \phi$, and a world $\Theta,\Phi$. Imagine the points exist in space and we're looking at them from a camera, and depending on how the world $\Theta,\Phi$ changes, the camera is looking at the origin from a different point.

Basically, I'm trying to render some objects on a computer in a roughly spherical arrangement, and allow the user to rotate the view.

Sort of like those animated, interactive keyword clouds you sometimes see on the Internet where you move the mouse and the keywords move around in a spherical arrangement.

First I calculate each point's ($p_i$) original $(x_i, y_i, z_i)$ like so:

\begin{align} x_i &= r_i * sin(\phi_i) * cos(\theta_i) \\ y_i &= r_i * sin(\theta_i) \\ z_i &= r_i * cos(\phi_i) * cos(\theta_i) \end{align}

Then, I rotate the original $(x_i,y_i,z_i)$ by the world $\Theta$ first about the $y$ axis:

\begin{align} x_i &:= cos(\Phi) * x_i - sin(\Phi) * z_i \\ y_i &:= y_i \\ z_i &:= sin(\Phi) * x_i + cos(\Phi) * z_i \end{align}

Then about the $x$ axis:

\begin{align} x_i &:= x_i \\ y_i &:= cos(\Theta) * y_i + sin(\Theta) * z_i \\ z_i &:= cos(\Theta) * z_i - sin(\Theta) * y_i \end{align}

I then render each point $p_i$ at co-ordinates $(x_i, y_i, z_i)$.

The problem is, as long as I only apply one of the two world rotations - either $\Theta$ or $\Phi$, everything is rendered correctly. As soon as I apply both rotations together, as the interactive image is displayed with $\Theta$ and $\Phi$ changing, the objects get distorted, as though they're being sheared into a plane, and then back to their original spherical arrangement again.

I'm not entirely sure what I'm doing wrong.

-
your $r,\theta,\phi$ seem to be different from the standard math usage. Perhaps if you link to or add a figure it could help. – Maesumi Jan 11 '13 at 4:03
@Maesumi $x$ is left/right, $y$ is up/down, $z$ is in/out. $\theta$ is rotation about the $x$ axis, and $\phi$ is rotation about the $y$ axis. $y$-rotation is applied first, then $x$-rotation. I don't have a figure illustrating this, but it shouldn't be too complicated? – user1002358 Jan 11 '13 at 4:20
I am not sure if this is a typo but on your second set of equations you have $x_i$ in terms of $x_i$ and $z_i$, while $z_i$ is in terms of $x_i$ and $y_i$ instead of $z_i$. – Maesumi Jan 11 '13 at 4:45
@Maesumi Thanks! You're right - but it was just a typo in my question. My code had the correct equations. – user1002358 Jan 11 '13 at 4:58
That is what I thought. Now in your second paragraph you say $\theta$ is rotation angle about $x$ axis. This is very confusing to me. So let's make sure we are talking about the same thing. Your first set of equations look like spherical coordinate formulas. Except for your formula for $y_i$. Are you using spherical coordinates? e.g. as in here. If so let me know how you relabel the first picture there to get yours. – Maesumi Jan 11 '13 at 5:25