[英]iOS OpenGL ES 2.0 Quaternions Rotation with Momentum after Swipe
我正在学习用于iOS的OpenGL ES 2.0,并且在几个tutuorial的帮助下可以使用四元数旋转和缩放一个简单的球体对象。 当用户在完成滑动后将手指从屏幕上抬起时,我希望地球能够以递减的速度继续旋转 - 所以我想给球体一些动力。 我用这个博客来了解旋转: 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,并围绕四元数轴旋转该矩阵,以获得一个值,该值从一个触发器更新和减少的计时器开始触发我的应用。 结果是旋转,但是轴看起来不是很正确(看起来很接近),并且角度会产生我所期望的相反旋转方向 - 所以如果我向下滑动并抬起我的手指,地球会以降低的速度向上旋转。 更改角度值的符号无济于事。 如何从四元数中正确提取旋转轴,以适当的角度旋转,并以递减的速度进行旋转? 谢谢 -
// 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);
您可以完全避免必须通过在UIKit中使用UIScrollView类来实现动量物理。 您可以定位实际上不在OpenGL视图上绘制任何内容的UIScrollView,并将其配置为具有所需的contentSize(可滚动区域)和滚动属性。 然后,您可以使其中一个类符合UIScrollViewDelegate ,并使该类的实例成为UIScrollView的委托。 然后,当用户与您的不可见滚动视图(在您的OpenGL视图顶部)进行交互时,您的滚动视图委托类将被通知用户的交互。 以下委托方法可能对实现特别有用。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
在那里,您可以获取新更新的contentOffset并使用它来更新3D模型,然后可以使用OpenGL将其渲染到OpenGL视图中。 所以你只是使用滚动视图作为检测用户交互的机制,它将处理iOS用户熟悉的所有漂亮的平滑更新和动量物理。
我强烈建议您与Josh Shaffer一起观看WWDC 2012 Session 223,“使用滚动视图增强用户体验” 。 在那次演讲中,他详细讨论了使用这种技术并举例说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.