If rotation matrices are not working out for you due to the gimbal lock problem or poor speed performance, you could consider using quaternions. The advantage is that the rotation operations are much faster and more accurate due to the rotations simply being multiplications without using trigonometric functions. Strangely, quaternions use a four dimensional vector with three imaginary numbers and one scalar, whose product is non-commutative. This book somehow does the impossible and clearly explains something that seems crazy. Additionally the book has some of the best figures for explaining Euler angles I have seen.