Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The question would not have arisen if CMDeviceMotion had an accessor for the userAcceleration in coordinates of the reference frame. So, I used a category to add the required method:</p> <p>In CMDeviceMotion+TransformToReferenceFrame.h:</p> <pre><code>#import &lt;CoreMotion/CoreMotion.h&gt; @interface CMDeviceMotion (TransformToReferenceFrame) -(CMAcceleration)userAccelerationInReferenceFrame; @end </code></pre> <p>and in CMDeviceMotion+TransformToReferenceFrame.m:</p> <pre><code>#import "CMDeviceMotion+TransformToReferenceFrame.h" @implementation CMDeviceMotion (TransformToReferenceFrame) -(CMAcceleration)userAccelerationInReferenceFrame { CMAcceleration acc = [self userAcceleration]; CMRotationMatrix rot = [self attitude].rotationMatrix; CMAcceleration accRef; accRef.x = acc.x*rot.m11 + acc.y*rot.m12 + acc.z*rot.m13; accRef.y = acc.x*rot.m21 + acc.y*rot.m22 + acc.z*rot.m23; accRef.z = acc.x*rot.m31 + acc.y*rot.m32 + acc.z*rot.m33; return accRef; } @end </code></pre> <p>and in Swift 3</p> <pre><code>extension CMDeviceMotion { var userAccelerationInReferenceFrame: CMAcceleration { let acc = self.userAcceleration let rot = self.attitude.rotationMatrix var accRef = CMAcceleration() accRef.x = acc.x*rot.m11 + acc.y*rot.m12 + acc.z*rot.m13; accRef.y = acc.x*rot.m21 + acc.y*rot.m22 + acc.z*rot.m23; accRef.z = acc.x*rot.m31 + acc.y*rot.m32 + acc.z*rot.m33; return accRef; } } </code></pre> <p>Now, code that previously used [deviceMotion userAcceleration] can use [deviceMotion userAccelerationInReferenceFrame] instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload