iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
4. Replacing Buttons with Orientation Sensors
The next step is carefully integrating support for the compass and accelerometer APIs. I say “carefully” because we’d like to provide a graceful runtime fallback if the device (or simulator) does not have a magnetometer or accelerometer.
We’ll be using the accelerometer to obtain the gravity vector, which in turn enables us to compute the phi angle (that’s “altitude” for you astronomers) but not the theta angle (azimuth). Conversely, the compass API can be used to compute theta but not phi. You’ll see how this works in the following sections.
4.1. Adding accelerometer support
Using the low-level accelerometer API directly is ill advised; the signal includes quite a bit of noise, and unless your app is somehow related to The Blair Witch Project, you probably don’t want your camera shaking around like a shivering chihuahua.
Discussing a robust and adaptive low-pass
filter implementation is beyond the scope of this book, but thankfully
Apple includes some example code for this. Search for the
AccelerometerGraph sample on the iPhone developer
site (http://developer.apple.com/iphone) and
download it. Look inside for two key files, and copy them to your
project folder: AccelerometerFilter.h and
AccelerometerFilter.m.
Example 10. Adding accelerometer support to GLView.h
Next, open GLView.mm, and add the lines shown in bold in Example 11. You might grimace at the sight of the #if block, but it’s a necessary evil because the iPhone Simulator pretends to support the accelerometer APIs by sending the application fictitious values (without giving the user much control over those values). Since the fake accelerometer won’t do us much good, we turn it off when building for the simulator.
Note:
An Egyptian software company called vimov produces a compelling tool called iSimulate that can simulate the accelerometer and other device sensors. Check it out at http://www.vimov.com/isimulate.
Example 11. Adding accelerometer support to initWithFrame
Since GLView sets itself as the accelerometer delegate, it needs to implement a response handler. See Example 12.
Example 12. Accelerometer response handler
You might not be familiar with the atan2 function, which takes the arctangent of the its first argument divided by the its second argument (see Phi as a function of acceleration). Why not use the plain old single-argument atan function and do the division yourself? You don’t because atan2 is smarter; it uses the signs of its arguments to determine which quadrant the angle is in. Plus, it allows the second argument to be zero without throwing a divide-by-zero exception.
Note:
An even more rarely encountered math function is hypot. When used together, atan2 and hypot can convert any 2D Cartesian coordinate into a polar coordinate.
Phi as a function of acceleration

Phi as a function of acceleration shows how we compute phi from the accelerometer’s input values. To understand it, you first need to realize that we’re using the accelerometer as a way of measuring the direction of gravity. It’s a common misconception that the accelerometer measures speed, but you know better by now! The accelerometer API returns a 3D acceleration vector according to the axes depicted in Figure 4.
Figure 4. Accelerometer axes in landscape mode

When you hold the device in landscape mode, there’s no gravity along the y-axis (assuming you’re not slothfully laying on the sofa and turned to one side). So, the gravity vector is composed of X and Z only—see Figure 5.
Figure 5. Computing phi from acceleration

4.2. Adding compass support
The direction of gravity can’t tell you which direction you’re facing; that’s where the compass support in third-generation devices comes in. To begin, open GLView.h, and add the bold lines in Example 13.
Example 13. Adding compass support to GLView.h
The Core Location API is an umbrella for both GPS and compass functionality, but we’ll be using only the compass functionality in our demo. Next we need to create an instance of CLLocationManger somewhere in GLview.mm; see Example 14.
Example 14. Adding compass support to initWithFrame
Similar to how it handles the accelerometer feedback, GLView sets itself as the compass delegate, so it needs to implement a response handler. See Example 6-31. Unlike the accelerometer, any noise in the compass reading is already eliminated, so there’s no need for handling the low-pass filter yourself. The compass API is embarrassingly simple; it simply returns an angle in degrees, where 0 is north, 90 is east, and so on. See Example 15 for the compass response handler.