Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you are not allowed to create an instance of CMAttitude on your own, the only way is to create a new quaternion and take this for any further calculations. I recommend using your own customised quaternions class instead of the simple struct <a href="http://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMAttitude_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009943-CH1-SW1" rel="nofollow">CMQuaternion</a>. A good starting point is the <a href="http://code.google.com/p/cocoamath/" rel="nofollow">cocoamath</a> project where you can take Quaternion.m, Quaternion.h and QuaternionOperations.m from <a href="http://code.google.com/p/cocoamath/source/browse/trunk" rel="nofollow">trunk</a> and I will use it for this sample code.</p> <p>First you have to save an instance from the <a href="http://content.gpwiki.org/index.php/OpenGL%3aTutorials%3aUsing_Quaternions_to_represent_rotation#The_complex_conjugate_of_a_quaternion" rel="nofollow">complex conjugate</a> of your reference quaternion (taken from <code>CMAttitude.quaternion</code>) to use for all subsequent multiplications i.e.</p> <p>MyClass.h</p> <pre><code>Quaternion* inverseReferenceQuaternion; </code></pre> <p>MyClass.c</p> <pre><code>// initialising code run only once after each call to [motionManager startDeviceMotionUpdates]; CMAttitude firstAttitude = [motionManager deviceMotion].attitude; CMQuaternion q = firstAttitude.quaternion; inverseReferenceQuaternion = [[Quaternion alloc] initWithRe:q.w i:-q.x j:-q.y k:-q.z]; </code></pre> <p>In your timer loop (or handler block) you need a <a href="http://content.gpwiki.org/index.php/OpenGL%3aTutorials%3aUsing_Quaternions_to_represent_rotation#Multiplying_quaternions" rel="nofollow">quaternion multiplication</a>:</p> <pre><code>// on every update CMAttitude attitude = [motionManager deviceMotion].attitude; CMQuaternion current = attitude.quaternion; Quaternion currentMultiplied = initAsProductOf:inverseReferenceQuaternion And:current </code></pre> <p>Now <code>currentMultiplied</code> should contain what you are looking for.</p> <p>Quaternions are pretty if you are new to this subject, but it's worth to read some tutorials like for example <a href="http://content.gpwiki.org/index.php/OpenGL%3aTutorials%3aUsing_Quaternions_to_represent_rotation" rel="nofollow">OpenGL:Tutorials:Using Quaternions to represent rotation</a> or <a href="http://www.gamedev.net/page/resources/_/technical/math-and-physics/quaternion-powers-r1095" rel="nofollow">Quaternion Powers</a>.</p>
    singulars
    1. This table or related slice is empty.
    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. 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