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 04-02-2009, 01:58 AM   #20 (permalink)
Alx
New Member
 
Join Date: Jan 2009
Posts: 11
Alx is on a distinguished road
Default

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
Alx is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,633
Threads: 94,090
Posts: 402,803
Top Poster: BrianSlick (7,990)
Welcome to our newest member, nor9849
Powered by vBadvanced CMPS v3.1.0

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