我正在关注四元数教程:http ://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl并试图将地球旋转到某个 XYZ 位置。我有一个初始四元数并在地球表面生成一个随机 XYZ 位置。我将该 XYZ 位置传递给以下函数。这个想法是用 GLKMatrix4MakeLookAt 生成一个lookAt向量,并从lookAt矩阵定义slerp步骤的结束四元数。
- (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];
}
插值有效,我得到了一个平滑的旋转,但是结束位置永远不是我输入的 XYZ - 我知道这一点,因为我的地球是一个球体,我正在从 Lat Lon 计算 XYZ。我想在旋转后从地球表面的纬度/经度位置直接向下看向地球中心的“lookAt”矢量。我认为这可能与向上向量有关,但我已经尝试了所有有意义的方法。
我在做什么错 - 如何定义一个最终四元数,当我完成旋转时,向下看一个向量到地球表面的 XYZ?谢谢!