2

So I have done a basic calibration in my game like so: Calibrate Code:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [[NSUserDefaults standardUserDefaults] setFloat:acceleration.x forKey:@"X-Calibrate"];
}

Then in my game view these are some defines for the accelerometer:

#define kFilteringFactor 0.13
#define MAXXACCELERATION 24

In the init method of my game class I do this:

calibrationFloat = [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"];

Then I do this:

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    //Low Pass Filter (gets rid of little jitters) + Calibration value combined
    rollingX = ((acceleration.x - calibrationFloat) * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
}

calibrationFloat is set to the value read from NSUserDefaults before the game starts.

Then in the game loop I do this:

int rollingAmount = (IS_IPAD() ? 52 : 44);
CGFloat xFloat = (rollingX * rollingAmount);
pos.x += xFloat < -MAXXACCELERATION ? -MAXXACCELERATION : (xFloat > MAXXACCELERATION ? MAXXACCELERATION : xFloat);

Then I set the position of my image based upon pos.x. This however is the problem: After I calibrate, the sensitivity of the movement of my image because very high making it very fast movements. Before I calibrate, the movement is at a nice pace so something with the calibration must be going wrong.

Does anything not seem right here, am I doing something wrong with the calculations?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • "calibrationValue is set to the value read from NSUserDefaults" I don't see `calibrationValue` anywhere in your code. – Victor Zamanian Dec 01 '12 at 21:29
  • I added it. Sorry about that! :) – SimplyKiwi Dec 01 '12 at 21:33
  • Okay, no problem. However, I still don't see that variable being used anywhere. Do you mean `calibrationFloat` instead or are those two different variables? (Also, I saw you asked a related question regarding this where an answer said you were doing the calculation for `rollingX` incorrectly. Have you ruled that out completely?) – Victor Zamanian Dec 01 '12 at 21:36
  • Yes my question used the wrong variable, I updated it. That answerer was for this question: http://stackoverflow.com/questions/13225713/calibrate-uiaccelerometer However, he did not provide a specific enough answer on how to fix the rollingX calculation. Also I know he says to calibrate on a 3D scale but in this case I only care about the X axis. So no, I have not fixed the rollingX calculation error because I am not sure where the error is or how to fix that. – SimplyKiwi Dec 01 '12 at 21:42
  • Where do you initialize `rollingX` for the very first time? – Victor Zamanian Dec 01 '12 at 21:57
  • In the game class's "fakeInit" (fakeInit is a method that is like a half init where it takes objects that are already created and repositions them set values to 0, etc... But is only called the second time the game view is loaded). So in the fakeInit I do: rollingX = 0; – SimplyKiwi Dec 01 '12 at 21:59
  • Okay. I was wondering if `rollingX` was uninitialized and was causing you to get large values for `xFloat`. But if `rollingX` is initialized to `0` before the calculations then that shouldn't be the problem. The only other thing I can see immediately is that `acceleration.x` is actually a `double`, but that shouldn't have too big of an impact either. So I'm afraid I'm out of ideas. Sorry. :-\ – Victor Zamanian Dec 01 '12 at 22:21
  • Darn are you sure you have no idea why my calculations are wrong for rollingX either? I think that is the base of the problem and we can work from there. – SimplyKiwi Dec 02 '12 at 07:58
  • I'm afraid I'm out of ideas. :/ I would have to look at your entire code base to get a better understanding in that case I think. But if I were you I would start logging the different values to the console to see where exactly these large accelerations are being generated. That way you can trace the problem more precisely. – Victor Zamanian Dec 02 '12 at 10:13

1 Answers1

1

try to add a synchronize, just to be sure you wrote a right value in NSUserDefaults:

'couse you cannot be sure that your setting is wrote to disk before you read it, so you'd better to force your app to write it right now

in apple doc:

synchronize Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

  • (BOOL)synchronize Return Value YES if the data was saved successfully to disk, otherwise NO.

and try to use setDouble: instead of setFloat (UIAccelerationValue x is defined as double... and of course use doubleForKey: when you read it later)

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [[NSUserDefaults standardUserDefaults] setDouble:acceleration.x forKey:@"X-Calibrate"];
    if ([[NSUserDefaults standardUserDefaults] synchronize]) {
        NSLog(@"OK: well written!!!");
    }else {
        NSLog(@"NO! Error!!!");
    }

}
meronix
  • 6,175
  • 1
  • 23
  • 36
  • Unfortunately that is not the problem, the value is correct, its just my calculations seem to be wrong and I am not sure what they should be instead. – SimplyKiwi Dec 05 '12 at 20:38