title: Arcball camera inverting at 90 deg azimuth
body: <p>I'm attempting to implement an arcball style camera. I use glm::lookAt to keep the camera pointed at a target, and then move it around the surface of a sphere using azimuth/inclination angles to rotate the view.</p>
  
  <p>I'm running into an issue where the view gets flipped upside down when the azimuth approaches 90 degrees.</p>
  
  <p>Here's the relevant code:</p>
  
  <ol>
  <li><p>Get projection and view martrices.  Runs in the main loop</p>
  
  <pre><code>void Visual::updateModelViewProjection()
  {
      model = glm::mat4();
      projection = glm::mat4();
      view = glm::mat4();
  
      projection = glm::perspective
          (
          (float)glm::radians(camera.Zoom),
          (float)width / height, // aspect ratio
          0.1f, // near clipping plane
          10000.0f // far clipping plane
          );
  
      view = glm::lookAt(camera.Position, camera.Target, camera.Up);
  }
  </code></pre></li>
  <li><p>Mouse move event, for camera rotation</p>
  
  <pre><code>void Visual::cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
  {
      if (leftMousePressed)
      {
         ...
      }
  
      if (rightMousePressed)
      {
          GLfloat xoffset = (xpos - cursorPrevX) / 4.0;
          GLfloat yoffset = (cursorPrevY - ypos) / 4.0;
  
          camera.inclination += yoffset;
          camera.azimuth -= xoffset;
          if (camera.inclination &gt; 89.0f)
              camera.inclination = 89.0f;
          if (camera.inclination &lt; 1.0f)
              camera.inclination = 1.0f;
  
          if (camera.azimuth &gt; 359.0f)
              camera.azimuth = 359.0f;
          if (camera.azimuth &lt; 1.0f)
              camera.azimuth = 1.0f;
  
          float radius = glm::distance(camera.Position, camera.Target);
          camera.Position[0] = camera.Target[0] + radius * cos(glm::radians(camera.azimuth)) * sin(glm::radians(camera.inclination));
          camera.Position[1] = camera.Target[1] + radius * sin(glm::radians(camera.azimuth)) * sin(glm::radians(camera.inclination));
          camera.Position[2] = camera.Target[2] + radius * cos(glm::radians(camera.inclination));
  
          camera.updateCameraVectors();
      }
  
      cursorPrevX = xpos;
      cursorPrevY = ypos;
  }
  </code></pre></li>
  <li><p>Calculate camera orientation vectors</p>
  
  <pre><code>void updateCameraVectors()
  {
      Front = glm::normalize(Target-Position);
      Right = glm::rotate(glm::normalize(glm::cross(Front, {0.0, 1.0, 0.0})), glm::radians(90.0f), Front);
      Up = glm::normalize(glm::cross(Front, Right));
  }
  </code></pre></li>
  </ol>
  
  <p>I'm pretty sure it's related to the way I calculate my camera's right vector, but I cannot figure out how to compensate.</p>
  
  <p>Has anyone run into this before?  Any suggestions? </p>