Search code examples
xcodecocos2d-iphonerotationaccelerometersmoothing

cocos2d :my rotation with the accelerometer is not smooth


so I use the accelerometer in cocos2d to rotate my sprite but the rotation isn't smooth at all . I know that I have to use filter but I don't know how to integrate it in my code :

-(id) init
{
self.isAccelerometerEnabled = YES;
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];

}

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

ombreoeuf1.rotation = acceleration.y * 90 ;

}

sorry for my english I'm french :/


Solution

  • Here's how to implement a lowpass filter. Experiment a bit with kFilteringFactor until you get nice results.

    // Declare an int `accelY` in your class interface and set it to 0 in init
    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
        float kFilteringFactor = 0.1;
        accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
        ombreoeuf1.rotation = accelY * 90;
    }