Here’s an excellent tutorial on how to compute device tilt using Core Motion and quaternions:
“Quatta-whatons”, you ask? Why:
The beauty of Quaternions
If you don’t know what a quaternion is yet, please don’t be afraid by this strange word that seems right out of Star Trek.
Quaternions were first described by Hamilton in 1843 and applied to mechanics in three-dimensional space.
It eases the way we deal with the orientation of a body in a 3D space, and is better suited than the Euler angles that Apple is computing for us because of three reasons :
- it’s easier to compose rotations or to extract values from it.
- it avoids the gimbal lock problem.
- and Apple provides a quaternion in the CMAttitude class instance.
And because we only want to compute the yaw we do not have to worry about the gimbal lock problem, since our goal is not to described the complete iPhone orientation in the 3D space but only the tilt…
Eyes glazing over yet? For a practical example of how to add some niftiness to your app with all this, check out
dulaccc / DPMeterView: “Gravity-aware gauge-style meter view with delightful animations.”