The reason Gimbal lock occurs is because when Euler angles are used to perform a rotation, each axis is processed independently, one after the other (e.g. X, Y then Z), leading to the possibility that an object could have its axis rotated such that by the time the last axis is processed one of the axis could be aligned to another, and due to the way Euler angles function, trying to perform a rotation while two axis are aligned will produce unexpected results.

For example, if we yaw (rotation about y-axis) 45° and then pitch (rotation about x-axis) -90° it is the equivalent of a pitch of -90° and then a roll (rotation about z-axis) of 45°. In fact, if the pitch rotation is ±90° it will cause the roll and yaw (z and x) axis to be aligned.

If this is hard to understand, try standing up, holding your arm stretched out so that it is parallel to the floor with your fingers extended (pointing away from you), now pitch your arm -90° (place your arm by your side, making your fingertips point towards the floor), now you can twist your arm (roll) without moving your body, but try walking around in a circle (keep your arm by your side) notice this is the same as rolling! But if you perform the same circle when your arm is out parallel to the ground it actually changes the direction your arm is pointing (yaw), you have just witnessed the y and z axis (yaw and roll) becoming aligned, a Gimbal Lock.

There is an interactive demo that can be used to demonstrate this at "http://gamemath.com/Euler.htm", try entering the angles as described to create a Gimbal lock and then try adjusting the "Heading" and "Bank" angles. Note: Heading = Yaw (y-axis), Bank = Roll (z-axis).

There are ways to keep Gimbal lock to a minimum when programming 3D rotations, if performing a rotation on only one axis, ensure it is the first axis to be processed, if performing two rotations, make sure they are the first and last axis to be processed.

To avoid Gimbal lock completely checks need to be placed in code whenever there is a chance of it occurring, or alternatively a different way of rotating can be used, specifically Quaternion Rotations. These types of rotation are confusing and hard to understand, but not particular hard to implement (see Quaternion).