title: Quaternion "lookAt" function
body: <p>Im struggling with the following problem. Im working with bone animation and I want (ie) the head of the player to follow an another object in space. My up axis is +Z my forward axis is +Y, and the magnitude of the quaternion is in W. I tried to use the mesa code for gluLookAt and use the 3x3 matrix to transform to a quaternion but it doesn't work as expected so I go in another direction...</p>
  
  <p>So far I got the following code that is "almost" working at least the head of the player is rotating (however the X angle seems to affect the Y rotation axis) in the good direction but its looking straight up instead on following an object on the floor at about 65 degree:</p>
  
  <pre><code>qt LookRotation( v3 lookAt, v3 upDirection )
  {
  qt t;
  
  v3 forward = lookAt;
  v3 up = upDirection;
  
  OrthoNormalize( &amp;forward, &amp;up );
  
  v3 right = v3_cross( up, forward );
  
  mat3 m = mat3_make( right.x, up.x, forward.x,
                      right.y, up.y, forward.y,
                      right.z, up.z, forward.z );
  
  t.w = sqrtf( 1.0f +
               m.r[ 0 ].x +
               m.r[ 1 ].y +
               m.r[ 2 ].z ) * 0.5f;
  
  float w4_recip = 1.0f / ( 4.0f * t.w );
  
  t.x = ( m.r[ 2 ].y - m.r[ 1 ].z ) * w4_recip;
  
  t.y = ( m.r[ 0 ].z - m.r[ 2 ].x ) * w4_recip;
  
  t.z = ( m.r[ 1 ].x - m.r[ 0 ].y ) * w4_recip;
  
  t = qt_normalize( t );
  
  return t;
  }
  </code></pre>
  
  <p>...
  ...
  ...</p>
  
  <pre><code>v3 v = v3_sub( vec4_to_v3( transform.world.r[ 3 ] /* The object XYZ location in the world */),
             skeleton-&gt;final_pose.location[ i ] /* i = The head joint location */ );
  
  v = v3_normalize( v );
  
  qt q = LookRotation( v,
          v3_make( 0.0f, 0.0f, 1.0f ) );
  </code></pre>
  
  <p>Can someone help me figuring out this problem... Im kinda new with quaternions and don't really know where I could have messed up. After quite some research basically what I want to do is something like the Unity API: <a href="http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html" rel="noreferrer">http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html</a></p>