Thanks for this tutorial.
I'm fighting with this Accelerometer since some days now.
I'm trying to move an object on the iphone's screen, by using the accelerometer.
When the iphone is flat, it's working fine.
Else, it's completely wrong.
I've to calibrate the iPhone.
But I can't understand how to do this.
Would someone please help me?
I'm running the app' in landscape mode.
Here is how I get the accelerometer values:
Code:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
accelerometerValues[0] = acceleration.x * kFilteringFactor + accelerometerValues[0] * (1.0 - kFilteringFactor);
accelerometerValues[1] = acceleration.y * kFilteringFactor + accelerometerValues[1] * (1.0 - kFilteringFactor);
accelerometerValues[2] = acceleration.z * kFilteringFactor + accelerometerValues[2] * (1.0 - kFilteringFactor);
}
Here is how I compute the new position of my object:
Code:
- (void) tick
{
if (gameFinished)
return;
float dTime;
CFTimeInterval time;
time = CFAbsoluteTimeGetCurrent();
dTime = time - lastTime;
float accelerationX = (accelerometerValues[1]) * dTime * 1000;
float accelerationY = (accelerometerValues[0]) * dTime * 1000;
float newX=m_TargetImgView.center.x+accelerationX;
float newY=m_TargetImgView.center.y+accelerationY;
if (newX < 0)
newX=0;
else if (newX > 480)
newX = 480;
if (newY < 0)
newY=0;
else if (newY > 320)
newY = 320;
m_myImgView.center=CGPointMake(newX,newY);
lastTime = time;
}
Thanks in advance for any help
Alx