This question already has an answer here:
I'm making a 3D game where the player can walk across the surface of a planet.
I am having a problem correctly rotating the camera's frame of reference, so that the camera is oriented correctly anywhere on the planet.
My strategy so far has been to create a quaternion that represents the rotation from an "up" vector (i.e. north pole) to the current position vector of the camera:
var up = new THREE.Vector3(0, 1, 0);
To get the final rotation of the camera, I apply the rotation of where the player is looking:
This means that wherever the player is, if they look down they will look towards the center of the planet.
I then apply that rotation to the
up vector to get the direction the player is looking:
var direction = up.clone().applyQuaternion(camera.quaternion);
Assuming the player is looking directly forward, if the player travels in that direction, I would expect them to walk around the planet in a straight line.
This works correctly near the north pole, however as I approach the south pole, the camera seems to rotate laterally (from the perspective of the player), finally as I reach the south pole the camera rapidly rotates 360 degrees around the pole.
What is causing this and how can I avoid it?