title: iOS OpenGL ES 2.0 Quaternion Rotation Slerp to XYZ Position
body: <p>I am following the quaternion tutorial: <a href="http://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl" rel="nofollow">http://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl</a> and am trying to rotate a globe to some XYZ location. I have an initial quaternion and generate a random XYZ location on the surface of the globe. I pass that XYZ location into the following function. The idea was to generate a lookAt vector with GLKMatrix4MakeLookAt and define the end Quaternion for the slerp step from the lookAt matrix.</p>
  
  <pre><code>- (void)rotateToLocationX:(float)x andY:(float)y andZ:(float)z {
  
      // Turn on the interpolation for smooth rotation
      _slerping = YES; // Begin auto rotating to this location
      _slerpCur = 0;
      _slerpMax = 1.0;
      _slerpStart = _quat;
  
      // The eye location is defined by the look at location multiplied by this modifier
      float modifier = 1.0;
  
      // Create a look at vector for which we will create a GLK4Matrix from
      float xEye = x;
      float yEye = y;
      float zEye = z;
      //NSLog(@"%f %f %f %f %f %f",xEye, yEye, zEye, x, y, z);
      _currentSatelliteLocation = GLKMatrix4MakeLookAt(xEye, yEye, zEye, 0, 0, 0, 0, 1, 0);
      _currentSatelliteLocation = GLKMatrix4Multiply(_currentSatelliteLocation,self.effect.transform.modelviewMatrix);
  
      // Turn our 4x4 matrix into a quat and use it to mark the end point of our interpolation
      //_currentSatelliteLocation = GLKMatrix4Translate(_currentSatelliteLocation, 0.0f, 0.0f, GLOBAL_EARTH_Z_LOCATION);
      _slerpEnd = GLKQuaternionMakeWithMatrix4(_currentSatelliteLocation);
  
      // Print info on the quat
      GLKVector3 vec = GLKQuaternionAxis(_slerpEnd);
      float angle = GLKQuaternionAngle(_slerpEnd);
      //NSLog(@"%f %f %f %f",vec.x,vec.y,vec.z,angle);
  
      NSLog(@"Quat end:");
      [self printMatrix:_currentSatelliteLocation];
      //[self printMatrix:self.effect.transform.modelviewMatrix];
  
  }
  </code></pre>
  
  <p>The interpolation works, I get a smooth rotation, however the ending location is never the XYZ  I input - I know this because my globe is a sphere and I am calculating XYZ from Lat Lon. I want to look directly down the 'lookAt' vector toward the center of the earth from that lat/lon location on the surface of the globe after the rotation. I think it may have something to do with the up vector but I've tried everything that made sense.</p>
  
  <p>What am I doing wrong - How can I define a final quaternion that when I finish rotating, looks down a vector to the XYZ on the surface of the globe? Thanks!</p>