Are you prepared for Wrath of the Lich King? WoW Insider has you covered!

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.

Related Headlines

Reader Comments (Page 1 of 1)

TUAW Features

back-to-school
Mac 101 ask-tuaw
Mac News
WWDC (251)
.Mac (66)
Accessories (652)
Airport (75)
Analysis / Opinion (1424)
Apple (1698)
Apple Corporate (580)
Apple Financial (200)
Apple History (51)
Apple Professional (54)
Apple TV (164)
Audio (450)
Bad Apple (131)
Beta Beat (155)
Blogging (87)
Bluetooth (19)
Bugs/Recalls (57)
Cult of Mac (879)
Deals (225)
Desktops (116)
Developer (282)
Education (110)
eMac (10)
Enterprise (147)
Features (412)
Freeware (399)
Gaming (397)
Graphic Design (38)
Hardware (1308)
Holidays (37)
Humor (589)
iBook (66)
iLife (240)
iMac (185)
Internet (340)
Internet Tools (1342)
iTS (982)
iTunes (823)
iWork (23)
Leopard (376)
Mac mini (112)
Mac Pro (54)
MacBook (206)
MacBook Air (83)
Macbook Pro (226)
MobileMe (47)
Multimedia (459)
Odds and ends (1489)
Open Source (282)
OS (940)
Peripherals (214)
Podcasting (183)
Podcasts (95)
Portables (198)
PowerBook (136)
PowerMac G5 (51)
Retail (612)
Retro Mac (50)
Rig of the Week (42)
Rumors (642)
Software (4459)
Software Update (428)
Steve Jobs (254)
Stocking Stuffers (50)
Surveys and Polls (98)
Switchers (114)
The Woz (35)
TUAW Business (257)
Universal Binary (281)
UNIX / BSD (61)
Video (908)
Weekend Review (84)
WIN Business (47)
Wireless (89)
Xserve (39)
iPhone/iPod News
iPhone (1789)
iPod Family (2118)
App Store (158)
SDK (29)
Mac Events
One More Thing (27)
Liveblog (2)
Other Events (226)
Macworld (489)
Mac Learning
AppleScript (4)
Ask TUAW (107)
Blogs (85)
Books (26)
Books and Blogs (62)
Cool tools (451)
Hacks (472)
How-tos (490)
Interviews (44)
Mods (191)
Productivity (591)
Reviews (114)
Security (167)
Terminal Tips (66)
Tips and tricks (575)
Troubleshooting (173)
TUAW Features
iPhone 101 (36)
TUAW Labs (4)
Blast From the Past (19)
TUAW Tips (150)
Flickr Find (38)
Found Footage (90)
Mac 101 (111)
TUAW Interview (31)
Widget Watch (198)
The Daily Best (1)
TUAW Faceoff (6)

RESOURCES

RSS NEWSFEEDS

Powered by Blogsmith

Sponsored Links

The Unofficial Apple Weblog (TUAW) bloggers (30 days)

#BloggerPostsCmts
1Cory Bohon561
2Robert Palmer5544
3Steven Sande5115
4Mike Schramm270
5Erica Sadun251
6Michael Rose2330
7Mat Lu2110
8Dave Caolo210
9Giles Turnbull200
10Christina Warren1428
11Brett Terpstra120
12TUAW Blogger70
13Victor Agreda, Jr.511
14Jason Clarke21
15Scott McNulty20

Featured Galleries

DNC Macs
Macworld 2008 Keynote
Macworld 2008 Build-up
Apple Vanity Plates
DiscPainter
Crash Bandicoot Nitro Kart 3D
Macworld Expo 2007 show floor
Apple Texas Hold 'Em
The Macworld Faithful in Line

 

    Most Commented On (7 days)

    Recent Comments

    More Apple Analysis

    More from AOL Money and Finance

    Other Weblogs Inc. Network blogs you might be interested in: