Skip to Content

Massively looks at the best free to play games
AOL Tech

Filed under: iPhone

iPhone Coding: Using the Accelerometer

You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication delegate lets you know whenever the iPhone has updated its tilt data.

When you add a method for (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz to any UIApplication, the iPhone sends regular messages to your application giving you values for X, Y, and Z. These values are not exactly what I first expected:

X = Roll X corresponds to roll, or rotation around the axis that runs from your home button to your earpiece. Values vary from 0.5 (rolled all the way to the left) to -0.5 (rolled all the way to the right).

Y = Pitch. Place your iPhone on the table and mentally draw a horizontal line about half-way down the screen. That's the axis around which the Y value rotates. Values go from 0.5 (the headphone jack straight down) to -0.5 (the headphone jack straight up).

Z = Face up/face down. I expected the Z value to correspond to yaw. And it does not. It refers to whether your iPhone is face up (-0.5) or face down (0.5). When placed on it side, either the side with the volume controls and ringer switch, or the side directly opposite, the Z value equates to 0.0.


I went to expert Nate True for some advice about these values. He explained the reason that Z is not yaw. It's because yaw is a compass value, something the iPhone can't measure. X, Y, and Z are linear acceleration values. They point to whichever way gravity is operating. Together, X, Y and Z form a 3-D acceleration vector that indicates the direction of gravity. "Say you're tossing the phone up into the air, then all the values will fall to 0 you could make an app that makes the phone scream if you drop it."

You can use the X and Y values to determine the highest point of the iPhone by using an arctan function, i.e. float angle = atan2(yy, xx);. I've put together a sample application that demonstrates this. As the screen shot here shows, it provides the raw X, Y and Z values and keeps the arrow pointing to whatever point is "up" for your iPhone.

Makefile

mainapp.m

SampleApp.h

SampleApp.m

If you'd like to play some more with the accelerometer, I've also thrown together a simple game called Butterfly that allows you to slide a net around your screen to catch a virtual monarch butterfly.

Click here to read all TUAW’s iPhone coverage

Reader Comments (Page 1 of 1)

Tip of the Day

To find out what version of Mac OS you are running, go to the Apple logo in the top left corner, click it and choose About This Mac. From that window you will see the version number, processor, memory and chosen startup disk. Clicking Software Update will check for updates, and More Info... will open up an extensive list of everything on your machine.


Follow us on Twitter!
TUAW [Cafepress]

Sponsored Links

Featured Galleries

DNC Macs
Macworld 2008 Keynote
Macworld 2008 Build-up
Google Earth for iPhone
Podcaster
AT&T Navigator Road Test
Bento for iPhone 1.0
Scrabble for iPhone
Tom Bihn Checkpoint Flyer Briefcase
Apple Vanity Plates
Apple booth Macworld 07
WorldVoice Radio
Quickoffice for iPhone 1.1.1
Daylite 3.9 Review
DiscPainter
Mariner Calc for iPhone
2009CupertinoBus
Crash Bandicoot Nitro Kart 3D
MLB.com At Bat 2009
Macworld Expo 2007 show floor
Apple Texas Hold 'Em

 

More Apple Analysis

AOL Radio TUAW on Stitcher