I use a rotation matrix :
R11 R12 R13
R21 R22 R23
R31 R32 R33
with R = Rz Ry Rx
if (R31 <> ±1)
y1 = -sin-1(R31)
y2 = pi + sin-1(R31)
x1 = atan2 (R32/cos y1,R33/cos y1)
x2 = atan2 (R32/cos y2,R33/cos y2)
z1 = atan2( R21/cos y1,R11/cos y1)
z2 = atan2( R21/cos y2,R11/cos y2)
Else
z= anything; can set to 0
if (R31 = -1)
y = -pi / 2
x = z + atan2(R12,R13)
Else
y = -pi / 2
x = -z + atan2(-R12,-R13)
End If
End If
or a simple version
result.X = Math.Atan2(R32, R33) * (180.0 / Math.PI)
result.Y = Math.Atan2(-1 * R31, Math.Sqrt(R32 * R32 + R33 * R33)) * (180.0 / Math.PI)
result.Z = Math.Atan2(R21, R11) * (180.0 / Math.PI)