我正在学习适用于 iOS 的 OpenGL ES 2.0,在几个教程的帮助下,可以使用四元数旋转和缩放一个简单的球体对象。当用户在完成滑动后从屏幕上抬起手指时,我希望地球继续以递减的速度旋转 - 所以我想给球体一些动力。我使用这个博客来了解旋转:http ://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl 。任何人都可以提供一些阅读或动力的例子吗?如何使用四元数实现动量?谢谢!
// Set up the frustrum and projection matrix
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
// Now perform the interpolation step
//[self slerpToNewLocation];
// Move the globe back so we can see it
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, GLOBAL_EARTH_Z_LOCATION);
// In update, we convert the quaternion into a rotation matrix, and apply it to the model view matrix as usual.
GLKMatrix4 rotation = GLKMatrix4MakeWithQuaternion(_quat);
GLKMatrix4 rotateMatrix = GLKMatrix4RotateWithVector3(rotation,momentumVar/200,GLKQuaternionAxis(_quat));
_quat = GLKQuaternionMakeWithMatrix4(rotateMatrix);
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, rotateMatrix);
// Cap the zoom scale factor max and mins
// only if slerping so that the auto-zoom out and back
// in still works during auto-rotation
if ( !_slerping ) {
if ( scale > 2.0 ) {
scale = 2.0;
}
if ( scale < 1.15 ) {
scale = 1.15;
}
}
// Apply the zoom factors
if ( _slerping ) {
//NSLog(@"final scale %f",scale);
}
modelViewMatrix = GLKMatrix4Scale(modelViewMatrix, scale, scale, scale);
// Assign the drawer modelViewMatrix
self.effect.transform.modelviewMatrix = modelViewMatrix;
我看到了以下问题:从两个四元数和时间步长获得动量四元数- 但不明白答案。我真的很感激这里的一些提示 - 谢谢。
// Called when touches are ended
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// The momentum var is initialized to a value
momentumVar = 0.025;
// Now since we stopped touching, decay the rotation to simulate momentum
momentumTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(decayGLKQuaternion)
userInfo:nil
repeats:YES];
}
// Slerp about the current axis after touches ended but for a greater angle ( radians )
- (void)decayGLKQuaternion {
if ( momentumVar < 0.001 ) {
// Stop the momentum and timer
momentumVar = 1.0;
[momentumTimer invalidate];
}
else {
// What is the current angle for the quaternion?
float currentQuatAngle = GLKQuaternionAngle(_quat);
// Decay the value each time
momentumVar = currentQuatAngle * 0.95;
}
}
更新:我仍在尝试解决这个问题,我尝试将更新方法中的当前四元数转换为 Matrix4 并围绕四元数轴旋转该矩阵以获得从开始触发的计时器更新和递减的值一次 TouchesEnded 内我的应用程序。结果进行了旋转,但轴看起来不太正确(看起来很接近),并且角度会产生与我预期相反的旋转方向 - 所以如果我向下滑动并抬起手指,地球会以降低的速度向上旋转。更改角度值的符号无济于事。如何从四元数中正确提取旋转轴,以适当的角度旋转它,并以递减的速度这样做?谢谢 -
// In update, we convert the quaternion into a rotation matrix, and apply it to the model view matrix as usual.
GLKMatrix4 rotation = GLKMatrix4MakeWithQuaternion(_quat);
GLKMatrix4 rotateMatrix = GLKMatrix4RotateWithVector3(rotation,momentumVar/200,GLKQuaternionAxis(_quat));
_quat = GLKQuaternionMakeWithMatrix4(rotateMatrix);
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, rotateMatrix);