title: Orbit camera around sphere while looking at the center (using quaternions)
body: <p>I have looked at a ton of quaternion examples on several sites including this one, but found none that answer this, so here goes...</p>
  
  <p>I want to orbit my camera around a large sphere, and have the camera always point at the center of the sphere. To make things easy, the sphere is located at {0,0,0} in the world. I am using a quaternion for camera orientation, and a vector for camera position. The problem is that the camera position orbits the sphere perfectly as it should, but always looks one constant direction straight forward instead of adjusting to point to the center as it orbits. </p>
  
  <p>This must be something simple, but I am new to quaternions... what am I missing?</p>
  
  <p>I'm using C++, DirectX9. Here is my code:</p>
  
  <pre><code>// Note: g_camRotAngleYawDir and g_camRotAnglePitchDir are set to either 1.0f, -1.0f according to keypresses, otherwise equal 0.0f
  // Also, g_camOrientationQuat is just an identity quat to begin with.
  
  float theta = 0.05f;
  D3DXQUATERNION g_deltaQuat( 0.0f, 0.0f, 0.0f, 1.0f );
  D3DXQuaternionRotationYawPitchRoll(&amp;g_deltaQuat, g_camRotAngleYawDir * theta, g_camRotAnglePitchDir * theta, g_camRotAngleRollDir * theta);
  D3DXQUATERNION tempQuat = g_camOrientationQuat;
  D3DXQuaternionMultiply(&amp;g_camOrientationQuat, &amp;tempQuat, &amp;g_deltaQuat);
  
  D3DXMatrixRotationQuaternion(&amp;g_viewMatrix, &amp;g_camOrientationQuat);
  g_viewMatrix._41 = g_camPosition.x;
  g_viewMatrix._42 = g_camPosition.y;
  g_viewMatrix._43 = g_camPosition.z;
  
  g_direct3DDevice9-&gt;SetTransform( D3DTS_VIEW, &amp;g_viewMatrix );
  </code></pre>
  
  <p>[EDIT - Feb 13, 2012]</p>
  
  <p>Ok, here's my understanding so far:</p>
  
  <p>move the camera using an angular delta each frame.
  Get a vector from center to camera-pos.
  Call quaternionRotationBetweenVectors with a z-facing unit vector and the target vector.
  Then use the result of that function for the orientation of the view matrix, and the camera-position goes in the translation portion of the view matrix.</p>
  
  <p>Here's the new code (called every frame)...</p>
  
  <pre><code>//  This part orbits the position around the sphere according to deltas for yaw, pitch, roll
  D3DXQuaternionRotationYawPitchRoll(&amp;deltaQuat, yawDelta, pitchDelta, rollDelta);
  
  D3DXMatrixRotationQuaternion(&amp;mat1, &amp;deltaQuat);
  
  D3DXVec3Transform(&amp;g_camPosition, &amp;g_camPosition, &amp;mat1);
  </code></pre>
  
  <p>//  This part adjusts the orientation of the camera to point at the center of the sphere</p>
  
  <pre><code>dir1 = normalize(vec3(0.0f, 0.0f, 0.0f) - g_camPosition);
  
  QuaternionRotationBetweenVectors(&amp;g_camOrientationQuat, vec3(0.0f, 0.0f, 1.0f), &amp;dir1);
  
  
  D3DXMatrixRotationQuaternion(&amp;g_viewMatrix, &amp;g_camOrientationQuat);
  
  g_viewMatrix._41 = g_camPosition.x;
  g_viewMatrix._42 = g_camPosition.y;
  g_viewMatrix._43 = g_camPosition.z;
  
  
  g_direct3DDevice9-&gt;SetTransform( D3DTS_VIEW, &amp;g_viewMatrix );
  </code></pre>
  
  <p>...I tried that solution out, without success. What am I doing wrong?</p>