I am learning OpenGL ES 2.0 for iOS and with the help of several tutuorials can rotate and zoom a simple sphere object using quaternions. I'd like the globe to continue rotating with diminishing speed when the user lifts their finger from the screen after completing a swipe - so I'd like to give the sphere some momentum. I used this blog to learn about rotating: http://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl. Can anybody provide some reading or examples of momentum? How can I implement momentum with quaternions? Thanks!
// 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;
I saw the following question: Obtaining momentum quaternion from two quaternions and timestep - but did not understand the answer. I'd really appreciate some hints here - thanks.
// 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;
}
}
UPDATE: I am still trying to figure this out, I have tried converting the current quaternion in the update method to a Matrix4 and rotate that matrix around the quaternion axis for a value that is updated and decremented from a timer that starts firing once TouchesEnded within my app. The result does a rotation, but the axis doesn't look quite right ( it seems close ) and the angle creates the opposite rotation direction that I would expect - so if I swipe down and lift my finger the globe rotates upward with decreasing speed. Changing the sign of the angle value does not help. How can I extract the rotation axis correctly from the quaternion, rotate it with the proper angle, and do so with decrementing speed? Thanks -
// 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);