Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-02-2010, 04:29 PM   #8 (permalink)
may
Registered Member
 
Join Date: Jan 2009
Posts: 18
may is on a distinguished road
Default

Quote:
Originally Posted by jmurff View Post
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.
__________________
our site at http://www.HamwayApps.net
may is offline   Reply With Quote
 

» Advertisements
» Online Users: 404
18 members and 386 guests
askb, Chickenrig, CMSLdesign, Code.ei, dansparrow, dre, guillermotricia, ilmman, jaychoupham, LEARN2MAKE, n00b, NSString, Paul Slocum, pbart, roof44, Speed, teebee74, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,591
Threads: 94,083
Posts: 402,777
Top Poster: BrianSlick (7,990)
Welcome to our newest member, guillermotricia
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:27 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.