Learn about the initial frame of reference used to report rotation and attitude values.
- Core Motion
The device attitude (or device orientation) reported by the device-motion service is always specified relative to a fixed reference frame. You decide which reference frame to use based on the interfaces you use to start the service. You can transform attitude values to other reference frames as needed.
CMAttitude type defines several fixed initial reference frames that you may use. Each reference frame assumes a device that is lying on a flat surface and is rotated in a particular direction. For example, the
x reference frame corresponds to a device whose x axis points toward magnetic north, as illustrated in Figure 1. Other reference frames assume a slightly different device orientation.
The attitude property of the
CMDevice object contains pitch, roll, and yaw values for the device that are relative to the reference frame. These values provide the device’s orientation in three-dimensional space. When all three values are 0, the device’s orientation matches the orientation of the reference frame. As the device rotates around each axis, the pitch, roll, and yaw values reflect the amount of rotation (in radians) around the given axis. Rotation values may be positive or negative and are in the range -∏ to ∏.
Figure 2 shows how pitch, roll, and yaw are reported relative to the initial reference frame. Pitch reports rotations around the device’s x axis, roll reports rotations around the y axis, and yaw reports rotations around the z axis.
For some types of apps, you can simplify your calculations by using the device’s current attitude as the starting point for tracking changes. For example, a baseball app that measures the user’s bat swing might use the current attitude as the starting bat position. As the user swings the bat, the app would then track only the changes from this initial start position.
Listing 1 shows how a baseball app would establish a new initial attitude for tracking the user’s bat swing. The
start method saves the device’s current attitude immediately before the swing begins. As the user swings, the
draw method multiplies the device’s current attitude by the inverse of the reference attitude, which yields the amount of change between the two attitudes. It then uses this transformed data to update the bat’s position onscreen.