0

I am trying to calibrate my UIAccelerometer from ViewA but my Game view is ViewB. So pretty much what I want to achieve is lets say the user is playing on their side, I want my game to act like if they were playing while sitting up in a normal position.

So in ViewA would I do something like so?:

float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"value1"]);
[[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"value1"];

Then in my Game class would I do this in my UIAccelerometer delegate method?:

float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
//Low Pass Filter
rollingX = (accelX * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
    AccelPoint.x += (rollingX*50);

Then I would do something like:

mySprite.position = ccp(accelX, yValue);

Am I doing anything wrong?

Thanks!

Edit: New code for ViewB, will I still need some form of friction now?

    rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));     

AccelPoint.x += (rollingX*50);

Then after that code I would set the position of my object.

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

1 Answers1

1

The example in this question appears to be calibrating in a similar manner to what you've described: Accelerometer & Calibration - iPhone SDK

Your example code for View A:

float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"value1"]);

[[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"value1"];

This will be storing a pre-calibrated value. So the effect of the calibration will be minimised if you've ever calibrated before.

I think just:

[[NSUserDefaults standardUserDefaults] setFloat:acceleration.x forKey:@"value1"];

Might be more what you're after. This way you'll be removing the entire effect of gravity in the user's starting position.

Your code for View B seems fine, although bear in mind this will allow the speed of your character to increase indefinitely. Consider adding some form of friction.

Community
  • 1
  • 1
Tom Elliott
  • 1,908
  • 1
  • 19
  • 39
  • AccelX is supposed to be accelX :) Anyway I think I only need calibration on the X-Axis since my game is only portrait and the character is only moved by the accelerometer on the X Axis. – SimplyKiwi Dec 01 '11 at 15:22
  • The only motion that the accelerometer handles for me is moving my character left or right and that's it. – SimplyKiwi Dec 01 '11 at 17:22
  • But what motion do you make to move your character left and right? – Tom Elliott Dec 01 '11 at 17:27
  • Right, with you now. Updated my answer with something you could try. – Tom Elliott Dec 02 '11 at 11:52
  • Ok. I don't want to remove the gravity though, I need to keep that! :) Also are you saying that if I calibrate again after another calibration, the old value will be overwritten? – SimplyKiwi Dec 02 '11 at 12:31
  • This won't remove the gravity entirely, just calibrate x to zero (I should point out I haven't tested this). If you calibrate again after calibration, you're in a totally different initial state, so the old value no longer applies. Overwriting any old calibration is fine. – Tom Elliott Dec 02 '11 at 15:18
  • Thanks! Also was my game class code ok for applying the nsuserdefaults calibrate value to the accelerometer? – SimplyKiwi Dec 02 '11 at 17:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5534/discussion-between-tom-elliott-and-ibrad-apps) – Tom Elliott Dec 02 '11 at 18:31
  • hmm there seems to be an issue, when I go and calibrate it, the Game view reflects as if the player was in the regular position rather than the calibrated position. Any ideas? – SimplyKiwi Dec 03 '11 at 02:40