【发布时间】:2013-08-01 17:12:59
【问题描述】:
我想在触球结束后获得动力。我在 iOS 上使用带有 Objective-C 和 OpenGL ES 2.0 API 的 GLKit。我正在使用四元数使用 Arcball Rotation 旋转以我的世界原点为中心的对象。当我触摸旋转时,我想继续以递减的速度旋转一段时间,直到没有旋转可做。
我的想法: 1.) 从四元数获取当前角度 2.) 将四元数转换为 4x4 矩阵 3.) 使用 GLKit 以从当前四元数减少的角度旋转 Matrix 4.) 在某些时候使计时器失效
我尝试在以 X、Y 和 Z 轴独立以及一起结束触摸结束后旋转矩阵,但结果旋转永远不会围绕我的四元数所期望的轴。当我用手指移动对象时,轨迹球旋转工作得很好——只有当手指松开时,旋转才会继续围绕某个看似任意的轴。当我抬起手指时,如何使轴与四元数旋转的最后状态一致?另外,我从文档中注意到,当对象逆时针旋转时,四元数的 GetAngle 函数应该返回弧度的负值。即使逆时针旋转,我也总是从这个函数中得到正值。
非常感谢您提供的任何见解!
// Called when touches are ended
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// The momentum var is initialized to a value
self.momentumVar = 0.05;
// Now since we stopped touching, decay the rotation to simulate momentum
self.momentumTimer = [NSTimer scheduledTimerWithTimeInterval:0.025
target:self
selector:@selector(decayGLKQuaternion)
userInfo:nil
repeats:YES];
}
// Rotate about the current axis after touches ended but for a greater angle ( radians )
- (void)decayGLKQuaternion {
//return;
// What is the current angle for the quaternion?
float currentQuatAngle = GLKQuaternionAngle(self.quat);
// Decay the value each time
self.momentumVar = currentQuatAngle * 0.0055;
NSLog(@"QUAT Angle %f %f",GLKQuaternionAngle(self.quat),GLKMathRadiansToDegrees(GLKQuaternionAngle(self.quat)));
GLKMatrix4 newMat = GLKMatrix4MakeWithQuaternion(self.quat);
float rotate = self.momentumVar * 0.75;
//newMat = GLKMatrix4RotateX(newMat, rotate);
//newMat = GLKMatrix4RotateY(newMat, rotate);
//newMat = GLKMatrix4RotateZ(newMat, rotate);
newMat = GLKMatrix4Rotate(newMat, rotate, 1, 0, 0);
newMat = GLKMatrix4Rotate(newMat, rotate, 0, 1, 0);
newMat = GLKMatrix4Rotate(newMat, rotate, 0, 1, 1);
self.quat = GLKQuaternionMakeWithMatrix4(newMat);
}
【问题讨论】:
-
我不是 iOS 方面的专家,但会不会是当你松开手指时,你的输入中出现了一些“跳跃”?当您将四元数保存几个状态然后将其用作参考点时会发生什么?
标签: objective-c opengl-es opengl-es-2.0 glkit quaternions