What formulas are required to calculate a 3d transformation?

52 Views Asked by At

Considering the point of view of square A and B, what math tranformations must be applied (either to the 3d camera or world) to transition from A to B?

I can tell that for the B viewpoint I had to move right and up, but I lack the math background to know what formulas can give me accurate values. Thanks

enter image description here

2

There are 2 best solutions below

2
On BEST ANSWER

A way to do it is to rotate the cube around the $z$ axis, then around the $x$ axis, with a $\pi/4$ rotation angle.

It means that if we define classically the rotation matrices around these resp. axes as:

$$R_z=\begin{pmatrix}\cos(a)& -\sin(a)& 0\\ \sin(a)& \cos(a)& 0\\ 0& 0& 1\end{pmatrix} R_x=\begin{pmatrix}1& 0& 0\\ 0& \cos(b)& -\sin(b)\\ 0& \sin(b)& \cos(b)\end{pmatrix},$$

with $a=b=\pi/4$, the rotation to be applied to the cube is the following product:

$$R=R_x R_z$$

(Recall: matrices must be applied from right to left), giving:

$$R \approx \begin{pmatrix}0.7071 & -0.7071 & 0\\ 0.5000 & 0.5000 & -0.7071\\ 0.5000 & 0.5000 & 0.7071 \end{pmatrix}$$

But, if we keep $a=\pi/4$ and take different values of $b$, it will make your cube do a kind of curtsy in front of you, helping you to select the most convenient $b$.

A particular case: $b=\operatorname{acos}(1/\sqrt{3})$, gives a position with 3 identical lozenges:

$$R \approx \begin{pmatrix}0.7071 & -0.7071 & 0\\ 0.4082 & 0.4082 & -0.8165\\ 0.5774 & 0.5774 & \ \ 0.5774\end{pmatrix}$$

0
On

I would recommend an approach where you first determine the initial basis unit vectors $\hat{u}$, $\hat{v}$, $\hat{w}$ and a fixed point $\vec{t}$, and corresponding final basis unit vectors $\hat{U}$, $\hat{V}$, $\hat{W}$ and a fixed point $\vec{T}$, such that the basis vectors are orthonormal, $$\begin{array}{rcr} \hat{u} \cdot \hat{u} = 1 & ~ & \hat{U} \cdot \hat{U} = 1 \\ \hat{u} \cdot \hat{v} = 0 & ~ & \hat{U} \cdot \hat{V} = 0 \\ \hat{u} \cdot \hat{w} = 0 & ~ & \hat{U} \cdot \hat{W} = 0 \\ \hat{v} \cdot \hat{u} = 0 & ~ & \hat{V} \cdot \hat{U} = 0 \\ \hat{v} \cdot \hat{v} = 1 & ~ & \hat{V} \cdot \hat{V} = 1 \\ \hat{v} \cdot \hat{w} = 0 & ~ & \hat{V} \cdot \hat{W} = 0 \\ \hat{w} \cdot \hat{u} = 0 & ~ & \hat{W} \cdot \hat{U} = 0 \\ \hat{w} \cdot \hat{v} = 0 & ~ & \hat{W} \cdot \hat{V} = 0 \\ \hat{w} \cdot \hat{w} = 1 & ~ & \hat{W} \cdot \hat{W} = 1 \\ \hat{u} \times \hat{v} = \hat{w} & ~ & \hat{U} \times \hat{V} = \hat{W} \\ \hat{w} \times \hat{u} = \hat{v} & ~ & \hat{W} \times \hat{U} = \hat{V} \\ \hat{v} \times \hat{w} = \hat{u} & ~ & \hat{V} \times \hat{W} = \hat{U} \\ \end{array}$$

Next, form two $4 \times 4$ transformation matrices: $$\mathbf{M}_\text{before} = \left[ \begin{matrix} u_x & v_x & w_x & t_x \\ u_y & v_y & w_y & t_y \\ u_z & v_z & w_z & t_z \\ 0 & 0 & 0 & 1 \\ \end{matrix} \right ], \quad \mathbf{M}_\text{after} = \left[ \begin{matrix} U_x & V_x & W_x & T_x \\ U_y & V_y & W_y & T_y \\ U_z & V_z & W_z & T_z \\ 0 & 0 & 0 & 1 \\ \end{matrix} \right ]$$ Then, you can calculate the transformation matrix $\mathbf{M}$ using $$\mathbf{M} = \mathbf{M}_\text{after} \mathbf{M}_\text{before}^{-1}$$ noting that because of orthonormality, $$\mathbf{M}_\text{before}^{-1} = \left[ \begin{matrix} v_y w_z - v_z w_y & v_z w_x - v_x w_z & v_x w_y - v_y w_x & t_x (v_z w_y - v_y w_z) + t_y (v_x w_z - v_z w_x) + t_z (v_y w_x - v_x w_y) \\ u_z w_y - u_y w_z & u_x w_z - u_z w_x & u_y w_x - u_x w_y & t_x (u_y w_z - u_z w_y) + t_y (u_z w_x - u_x w_z) + t_z (u_x w_y - u_y w_x) \\ u_y v_z - u_z v_y & u_z v_x - u_x v_z & u_x v_y - u_y v_x & t_x (u_z v_y - u_y v_z) + t_y (u_x v_z - u_z v_x) + t_z (u_y v_x - u_x v_y) \\ 0 & 0 & 0 & 1 \\ \end{matrix} \right]$$ but this also applies to non-orthonormal basis vector sets, as long as the initial vector $\vec{u}$ corresponds to final vector $\vec{U}$, initial vector $\vec{v}$ to final $\vec{V}$, initial $\vec{w}$ to final $\vec{W}$, they are all nonzero, and initial vector $\vec{t}$ corresponds to final vector $\vec{T}$.

The resulting transformation matrix $\mathbf{M}$ is applied to point $\vec{p} = ( x, y, z )$, transforming it to $\vec{q} = ( \chi , \gamma, \zeta )$, via $$\left[\begin{matrix} \chi \\ \gamma \\ \zeta \\ 1 \end{matrix} \right] = \mathbf{M} \left[ \begin{matrix} x \\ y \\ z \\ 1 \end{matrix} \right]$$ and represents both rotation and translation: the upper left $3\times 3$ matrix of $\mathbf{M}$ is a rotation matrix, and upper right $3 \times 1$ vector is the translation after rotation.

These $4\times 4$ matrices are extremely common in 3D graphics, in e.g. OpenGL.