Created
May 16, 2011 12:27
-
-
Save jnutting/974355 to your computer and use it in GitHub Desktop.
accelerometer smoothing delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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