Quote:
Originally Posted by jmurff
Hi;
Thought I'd post my example using the tutorial. Thanks for the tutorial.
I spent days researching and experimenting to come up with this combination of stuff. This works pretty well. I haven't however been able to get calibration to work. I did add a threshold value to stop the jitter. Any suggestions on Calibration or Jitter are appreciated.
-Jim
Code:
//
// REFERENCES::
// http://iphonedevelopertips.com/user-interface/accelerometer-101.html
// http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/39833-tutorial-accelerometer-calibration-optimizations.html
//
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// in landscape left mode so the Y acceleration
// value is actually our X value and vise versus.
// direction = -1 for normal controls or 1 for inverted
// sensitivity can be whatever value works for your game.
// choose values like 20 for low, 45 for med, and 70 for high.
// High Level Filter.
double tempPrevX = (acceleration.y * direction * kFilteringFactor) +
(prevX * (1.0 - kFilteringFactor));
double tempPrevY = (acceleration.x * -direction * kFilteringFactor) +
(prevY * (1.0 - kFilteringFactor));
double tempAccelX = (acceleration.y * direction) - tempPrevX;
double tempAccelY = (acceleration.x * -direction) - tempPrevY;
// NOTE:: Can't get this to work
// To create the offset do the following:
// 1. Start with the calibration the user has set.
// 2. Multiply the calibration by the sensitivity (taking into account whether
// we're using normal or inverted controls).
// 3. Keep in mind that the sensitivity will always be a positive value. However,
// when the controls are inverted we need to make this a NEGATIVE value.
// This is the reason we multiply the direction * -1 (remember,
// the direction = -1 when we're playing normal, and 1 when we're inverted).
float offsetX = (float)calibrationX * (sensitivity * ( direction * -1 ));
float offsetY = (float)calibrationY * (sensitivity * ( -direction * -1 ));
float movementX = ((float)tempAccelX * sensitivity) + offsetX;
float movementY = ((float)tempAccelY * sensitivity) + offsetY;
// To stop jitter check for a threshold of movement.
// If doesn't make jitter thresh leave old values in place.
// Setting it to 1 or 2 Pixels
BOOL changeInX = NO;
BOOL changeInY = NO;
if (movementX > kDefMovementThreshX || movementX < -kDefMovementThreshX)
{
ratSprite.position = ccp(ratSprite.position.x + movementX,
ratSprite.position.y);
prevX = tempPrevX;
accelX = tempAccelX;
changeInX = YES;
}
if (movementY > kDefMovementThreshY || movementY < -kDefMovementThreshY)
{
ratSprite.position = ccp(ratSprite.position.x,
ratSprite.position.y + movementY);
prevY = tempPrevY;
accelX = tempAccelY;
changeInY = YES;
}
// Only do this if needed
if (changeInX || changeInY)
{
//NSLog(@"movementX: %f, movementY: %f",movementX,movementY);
[self boundryCheck];
}
}
|
Hi thought i could add a little bit of help to the subject.
This is how i handle the jitters. Basically you have to do a little math / average the value of the reading. like this
Code:
- (float) addValueAndAverage: (float*)valueArray: (float) value
{
if (valueArray == nil ){
return value;
}
// cap arraySize to size of the array
if ( insertAt >= maxaccumulatedCount ){
return value;
}
float accumulator = 0.0;
int i = 0;
// stick the new value in
valueArray[insertAt] = value;
// add up, and shift the values down if full
for (i = 0; i < accumulatedCount; i++){
accumulator += valueArray[i];
}
return accumulator / accumulatedCount;
}
then in the accel call run something like this.
Code:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if ( accumulatedCount < maxaccumulatedCount){
accumulatedCount++;
}
float newaccelerationX = [self addValueAndAverage:accelXPast :acceleration.x];
if (fabs((fabs(newaccelerationX) - fabs(accelerationX))) >= 0.005){
accelerationX = newaccelerationX;
}
}
insertAt = (insertAt+1) % maxaccumulatedCount;
}
Hope this helps someone.