views:

680

answers:

2

I want to be able to make image move realistically with the accelerometer controlling it, like any labyrinth game. Below shows what I have so far but it seems very jittery and isnt realistic at all. The ball images seems to never be able to stop and does lots of jittery movements around everywhere.

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

deviceTilt.x = 0.01 * deviceTilt.x + (1.0 - 0.01) * acceleration.x;
deviceTilt.y = 0.01 * deviceTilt.y + (1.0 - 0.01) * acceleration.y;
}

-(void)onTimer {

    ballImage.center = CGPointMake(ballImage.center.x + (deviceTilt.x * 50), ballImage.center.y + (deviceTilt.y  * 50));

    if (ballImage.center.x > 279) {

        ballImage.center = CGPointMake(279, ballImage.center.y);
    }
    if (ballImage.center.x < 42) {

        ballImage.center = CGPointMake(42, ballImage.center.y);
    }
    if (ballImage.center.y > 419) {

        ballImage.center = CGPointMake(ballImage.center.x, 419);
    }
    if (ballImage.center.y < 181) {

        ballImage.center = CGPointMake(ballImage.center.x, 181);
    }
+1  A: 

You need to calculate the running average of the values. To do this you need to store the last n values in an array, and then push and pop values off the array when ever you read the accelerometer data. Here is some pseudocode:

const SIZE = 10;
float[] xVals = new float[SIZE];

float xAvg = 0;

function runAverage(float newX){
  xAvg += newX/SIZE;
  xVals.push(newX);
  if(xVals.length > SIZE){
    xAvg -= xVals.pop()/SIZE;
  }
}

You need to do this for all three axis. Play around with the value of SIZE; the larger it is, the smoother the value, but the slower things will seem to respond. It really depends on how often you read the accelerometer value. If it is read 10 times per second, then SIZE = 10 might be too large.

Marius
A: 

Is there some reason why you can not use the smoothing filter provided in response to your previous question: http://stackoverflow.com/questions/2272527 ?

Paul R
its a different question, that smoothed out the general movement and was trying out something someone suggested but it didnt work as i wanted because the smaller movements are really gittery
DotSlashSlash