Rotate camera around origin problem

Hey guys!

I’m trying to rotate a camera around the global origin, as if it was moving on a sphere, maintaining the same length to the origin at all times. Like the movement that is typical for any 3D model-viewer.

It works great when just rotating around the y-axis, pointing upwards.

camera.rotateAround(
    				new Vector3(0f, 0f, 0f), 
    				new Vector3(0f,1f,0f), 
    				1f);

The API is like this, for reference:

camera.rotateAround(
    				point, 
    				axis, 
    				degrees);

However, when I want the camera to rotate upwards or downwards, I need to take care that the camera might already be rotated. Therefore, I can’t just rotate around the x- og z-axis. I tried finding the perpendicular vector in 2D and rotating it around that, and that worked!

camera.rotateAround(
    				new Vector3(0f, 0f, 0f), 
    				new Vector3(
    						-camera.position.z,
    						0f,
    						camera.position.x), 
    				1f);

Here’s the problem though: Whenever my camera is directly above or directly below the origin, this math breaks and I can no longer rotate. I’d like to, though. How can I solve this problem, so it doesn’t break in these two cases?