Camera Rotation on Surface of a Sphere

I have a very large sphere that I would like the view camera to be able to fly on (the surface) as if it were a flat plane. Yet rotating the camera on the surface of the sphere has proven troublesome…Here is a quick video meant to demonstrate the issue:

The first seven seconds are spent showing the rotation error when the axes of the camera have not been modified. At seven seconds, the following line of code is activated:
Vector3f up = cam.getLocation().normalize();
cam.setAxes(up.cross(cam.getDirection()).normalize(), up, cam.getDirection());
and repeatedly executed in the update-loop. Since the camera is on a sphere I figured one can just set the up-vector to be the vector from the center of the sphere to the camera (just the camera position normalized.) But as shown by the video, this does not work.
I do have a more free range of rotation when using the aforementioned line of code, but as seen, the camera does not rotate all the way around (noticed when the camera begins to “jitter”.) I also cannot look up or down when using that code: when I attempt to “look” up and down, the camera simply rotates.

Is there anyway to fix this problem?


The BetterCharacter has some code to deal with this issue. Its not as easily solved as it may seem due to complicated stuff like gimbal lock and nasty singularities in the quaternions and things like that. Its mostly commented in BetterCharacter I think.