Rotating co-ordinates in 3D

942 Views Asked by At

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.