Quote:
Originally Posted by Alx
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
|
Hello Friend...
I want to tht I have one object in iphone when i move my iphone in right side then tht object is move in right side same as tht is working in left side also
i'm doing some experiment ... but i'm fail...
- (void)accelerometer

UIAccelerometer *)accelerometer didAccelerate

UIAcceleration *)acceleration {
// Get the current device angle
float xx = -[acceleration x];
float yy = [acceleration y];
// Read my blog for more details on the angles. It should be obvious that you
// could fire a custom shouldAutorotateToInterfaceOrientation-event here.
float angle = atan2(yy, xx);
// Add 1.5 to the angle to keep the label constantly horizontal to the viewer.
[myImage setTransform:CGAffineTransformMakeRotation(angle+1 .5)];
// if(y > 0.3) //Left
// {
// myImage.frame = myImage.frame.origin.x - 2, myImage.frame.origin.y;
// //playertank.position = cpv(playertank.position.x - 2, playertank.position.y);
// }
// else if(y < -0.3) //Right
// {
// playertank.position = cpv(playertank.position.x + 2, playertank.position.y);
// }
// else if(x > -0.3)//up
// {
// playertank.position = cpv(playertank.position.x, playertank.position.y + 2);
// }
// else if(x < -0.7)//Down
// {
// playertank.position = cpv(playertank.position.x , playertank.position.y - 2);
// }
//}
//
if(angle >= -2.25 && angle <= -0.25)
{
if(deviceOrientation != UIInterfaceOrientationPortrait)
{
deviceOrientation = UIInterfaceOrientationPortrait;
//[button1 setText:@"UIInterfaceOrientationPortrait"];
}
}
else if(angle >= -1.75 && angle <= 0.75)
{
if(deviceOrientation != UIInterfaceOrientationLandscapeRight)
{
deviceOrientation = UIInterfaceOrientationLandscapeRight;
//[interfaceOrientationLabel setText:@"UIInterfaceOrientationLandscapeRight"];
}
}
else if(angle >= 0.75 && angle <= 2.25)
{
if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)
{
deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;
//[interfaceOrientationLabel setText:@"UIInterfaceOrientationPortraitUpsideDown"];
}
}
else if(angle <= -2.25 || angle >= 2.25)
{
if(deviceOrientation != UIInterfaceOrientationLandscapeLeft)
{
deviceOrientation = UIInterfaceOrientationLandscapeLeft;
//[interfaceOrientationLabel setText:@"UIInterfaceOrientationLandscapeLeft"];
}
}
}
so plz help me friend it's urgent....