Search code examples
iosswiftframeaccelerometer

Phone acceleration relative to initial reference of frame in iOS


Can I somehow get acceration vector relative to initial reference of frame at any time? (what I mean: reference of frame in xArbitraryZVertical mode, at the first time I got Core Motion data) What I tried to do: Everytime I get CM data I multiply new rotation matrix (which locate in attitude property) by old rotation matrix (matrix I got at previous steps by multiplying over and over rotation matrices) and multiply this by acceleration vector. But it doesn't seem to work. Maybe it's a bit silly question, sorry then, but I really don't know how to do what I want. And maybe there is a simpler way to do this?


Solution

    1. Poll the motion manager's deviceMotion repeatedly.

    2. Each time you receive nil, return without doing anything.

    3. The first time you receive a non-nil result, save its attitude to an instance property.

    4. Every subsequent time you receive a non-nil result, call multiply(byInverseOf:) on its attitude, with the stored attitude as the argument. The result is the attitude change from the original.