Let's say we have an object in 3D space located at P(1,1,1) and if we decided we wanted to rotate this object within any of the planes XY
, XZ
, YZ
along any of the 3 axis where this is a basic 3D Cartesian system in a Right-Hand System the rotation matrices $R_n$ for P will look like these:
$$ R_x \space|| \space R_y \space || \space R_z $$
$$R_x(\theta) P =
\begin{bmatrix}
1 & 0 & 0 \\
0 & \cos\theta & -\sin\theta \\
0 & \sin\theta & \space\space\cos\theta \\
\end{bmatrix}
\begin{bmatrix}
P_x \\
P_y \\
P_z \\
\end{bmatrix},$$
$$R_y(\theta) P =
\begin{bmatrix}
\space\cos\theta & 0 & \sin\theta \\
0 & 1 & 0 \\
-\sin\theta & 0 & \cos\theta \\
\end{bmatrix}
\begin{bmatrix}
P_x \\
P_y \\
P_z \\
\end{bmatrix},$$
$$R_z(\theta) P=
\begin{bmatrix}
\cos\theta & -\sin \theta & 0 \\
\sin\theta & \space\space\cos \theta & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\begin{bmatrix}
P_x \\
P_y \\
P_z \\
\end{bmatrix}$$
When doing rotations in 3D among any of the arbitrary axis, the order of the rotations, the handedness of the system, the direction of the rotations and doing rotations among multiple axis does matter. To demonstrate this let's say angle $\theta = 90°$ and we apply this rotation consecutively in multiple dimensions you will see that we eventually end up with Gimbal Lock. First we will do $R_x$ by 90° then $R_y$ and finally try $R_z$
Here we are going to apply a 90° rotation to the point or vector P(1,1,1) on the $R_x$ axis
$R_x(90°)$
$\begin{bmatrix}
1 \\
1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
1 & 0 & 0 \\
0 & \cos 90° & -\sin 90° \\
0 & \sin 90° & \space\space\cos 90° \\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
1 & 0 & \space\space 0 \\
0 & 0 & -1 \\
0 & 1 & \space\space 0 \\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
1 \\
-1 \\
1 \\
\end{bmatrix}$
Now that our vector P has been transformed we will apply another 90° rotation but this time to the $R_y$ axis with the new values.
$R_y(90°)$
$\begin{bmatrix}
1 \\
-1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
\cos 90° & 0 & \sin 90°\\
0 & 1 & 0 \\
-\sin 90° & 0 & \cos 90°\\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
-1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
0 & 0 & 1 \\
0 & 1 & 0 \\
-1 & 0 & 0\\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
-1 \\
1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
1 \\
-1 \\
-1 \\
\end{bmatrix}$
We can now finish with $R_z$
$R_z(90°)$
$\begin{bmatrix}
1 \\
-1 \\
-1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
\cos 90° & -\sin 90° & 0 \\
\sin 90° & \cos 90° & 0 \\
0 & 0 & 1 \\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
-1 \\
-1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
0 & -1 & 0 \\
1 & 0 & 0 \\
0 & 0 & 1 \\
\end{bmatrix}$
$\begin{bmatrix}
1 \\
-1 \\
-1 \\
\end{bmatrix}$ =
$\begin{bmatrix}
1 \\
1 \\
-1 \\
\end{bmatrix}$
And as you can see in the calculations of the matrices there has been a change in direction for each time we rotated by 90 degrees. Here we have lost a degree of freedom of rotation. What this means is we rotated the X components by 90° which happens to be perpendicular or orthogonal to both the Y & Z axis which is evident of the fact that $\cos(90°) = 0$. Then when we rotate again by 90° along the Y axis and once again the Y axis is perpendicular to both the X & Z axis now we have 2 axis of rotations that are aligned so when we try to rotate in the 3rd dimension of space we lost a degree of freedom because we can no longer distinguish between the X & Y as they will both rotate simultaneously and there is no method to separate them. This can be seen from the calculations that were done by the matrices. It may not be completely evident now, but if you were to do all 6 permutations of the order of axis rotations you will see the pattern emerge. These kind of rotations are called Euler Angles.
It also doesn't matter what combination of axis you rotate with because it will happen with every combination when two axis of rotations become parallel.
$$R_x(90°)P \to R_y(90°)P \to R_z(90°)P \implies Gimbal Lock$$
$$R_x(90°)P \to R_z(90°)P \to R_y(90°)P \implies Gimbal Lock$$
$$R_y(90°)P \to R_x(90°)P \to R_z(90°)P \implies Gimbal Lock$$
$$R_y(90°)P \to R_z(90°)P \to R_x(90°)P \implies Gimbal Lock$$
$$R_z(90°)P \to R_x(90°)P \to R_y(90°)P \implies Gimbal Lock$$
$$R_z(90°)P \to R_y(90°)P \to R_x(90°)P \implies Gimbal Lock$$
If I simplify this by showing all 6 combinations with the ending transformation vectors or matrices for that same point you should see the pattern and these transformations are:
$$R_x(90°) P(1,1,1) \to
\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}
R_y(90°) \to
\begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix}
R_z(90°) \to
\begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}$$
$$R_x(90°) P(1,1,1) \to
\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}
R_z(90°) \to
\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}
R_y(90°) \to
\begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}$$
$$R_y(90°) P(1,1,1) \to
\begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}
R_x(90°) \to
\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}
R_z(90°) \to
\begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}$$
$$R_y(90°) P(1,1,1) \to
\begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}
R_z(90°) \to
\begin{bmatrix} -1 \\ 1 \\ -1 \\ \end{bmatrix}
R_x(90°) \to
\begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}$$
$$R_z(90°) P(1,1,1) \to
\begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}
R_x(90°) \to
\begin{bmatrix} -1 \\ -1 \\ 1 \\ \end{bmatrix}
R_y(90°) \to
\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$$
$$R_z(90°) P(1,1,1) \to
\begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}
R_y(90°) \to
\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}
R_x(90°) \to
\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$$
If you look at the combinations of the axis that you started with it doesn't matter the order of which two you finished with because the result will be the same for that starting axis of rotation. So intuitively we can say this about using Euler Angles of Rotation in 3D while considering the handedness of the coordinate system; the handedness matters because it will change the rotation matrices along with the trig functions and their signs for they will be different and so will your results. Now as for this particular coordinate system we can visually conclude this about Euler Angles:
$$R_x(\theta) P \implies \begin{bmatrix} a \\ a \\ -a \\ \end{bmatrix}$$
$$R_y(\theta) P \implies \begin{bmatrix} -a \\ a \\ a \\ \end{bmatrix}$$
$$R_z(\theta) P \implies \begin{bmatrix} a \\ -a \\ a \\ \end{bmatrix}$$
It may not seem quite apparent by the numbers as to what exactly is causing the gimbal lock, but the results of the transformations should give you some insight to what is going on. It might be easier to visualize than just by looking at the math. So I provided a link to a good video below. Now if you are interested in proofs then you have plenty of work ahead of you for there are also some other factors that cause this to happen such as the relationships of the $\cos(\theta)$ between two vectors being equal to the dot product between those vectors divided by their magnitudes.
$$\cos(\theta) = \frac{ V_1 \cdot V_2}{ \lvert V_1 \rvert \lvert V_2 \rvert } $$
Other contributing factors are the rules of calculus on the trigonometric functions especially the $\sin$ and $\cos$ functions.
$$(\sin{x})' = \cos{x}$$
$$(\cos{x})' = -\sin{x}$$
$$\int \sin{ax} \space dx = -\frac{1}{a}\cos{ax} + C$$
$$\int \cos{ax} \space dx = \frac{1}{a}\sin{ax} + C$$
Another interesting fact that I think that may lead to the reasoning of Gimbal Lock is quite interesting but that is a topic for another day as that in itself would merit its own page, but do forgive me if the math formatting isn't perfect I am new to this particular stack exchange site and I'm learning the math tags and formatting as I go.
Here is an excellent video illustrating Gimbal Lock: Youtube : Gimbal Lock