Skip to content

Instantly share code, notes, and snippets.

@jnutting
Created May 16, 2011 12:27
Show Gist options
  • Save jnutting/974355 to your computer and use it in GitHub Desktop.
Save jnutting/974355 to your computer and use it in GitHub Desktop.
accelerometer smoothing delegate
/*
* This gives something resembling an average of the last 1/k values. A k-value of 0.5 will
* give the current values equal weight with the previous smooth values, while a k-value of
* 0.9 will let the current values only account for 10% of the new smooth value.
*
* This approach probably consumes a little less memory, and calculates a little faster,
* than actually keeping an array of old values.
*/
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
CGFloat x = acceleration.x;
CGFloat y = acceleration.y;
CGFloat k = 0.5;
// smoothX and smoothY are ivars (CGFloat)
smoothX = k * smoothX + (1.0 - k) * x;
smoothY = k * smoothY + (1.0 - k) * y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment