greg rock
greg rock

Reputation: 239

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 :/

Upvotes: 1

Views: 1286

Answers (2)

s1m0n
s1m0n

Reputation: 7685

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;
}

Upvotes: 2

kenamarit
kenamarit

Reputation: 1

One thing that might help You with the smoothness is to set the update interval to 30 fps instead of 60, so update your init to:

-(id) init 
{
    self.isAccelerometerEnabled = YES;

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30];
}

Upvotes: 0

Related Questions