Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>In your case we can say rotation of the device is equal to rotation of the device normal (rotation around the normal itself is just ignored like you specified it)</li> <li><a href="http://developer.apple.com/library/ios/#documentation/CoreMotion/Reference/CMAttitude_Class/Reference/Reference.html" rel="nofollow">CMAttitude</a> which you can get via <a href="http://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMMotionManager_Class/Reference/Reference.html#//apple_ref/occ/instp/CMMotionManager/deviceMotion" rel="nofollow">CMMotionManager.deviceMotion</a> provides the rotation <strong>relative to a reference frame</strong>. Its properties quaternion, roation matrix and Euler angles are just different representations.</li> <li>The reference frame can be specified when you start device motion updates using CMMotionManager's <a href="http://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMMotionManager_Class/Reference/Reference.html#//apple_ref/occ/instm/CMMotionManager/startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:" rel="nofollow">startDeviceMotionUpdatesUsingReferenceFrame</a> method. Until iOS 4 you had to use <a href="http://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMAttitude_Class/Reference/Reference.html#//apple_ref/occ/instm/CMAttitude/multiplyByInverseOfAttitude:" rel="nofollow">multiplyByInverseOfAttitude</a></li> </ol> <p>Putting this together you just have to multiply the quaternion in the <em>right way</em> with the normal vector when the device lies face up on the table. Now we need this <em>right way</em> of quaternion multiplication that represents a rotation: According to <a href="http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation#Rotating_vectors" rel="nofollow">Rotating vectors</a> this is done by:</p> <p><strong>n = q * e * q'</strong> where <strong>q</strong> is the quaternion delivered by CMAttitude [w, (x, y, z)], <strong>q'</strong> is its conjugate [w, (-x, -y, -z)] and <strong>e</strong> is the quaternion representation of the face up normal [0, (0, 0, 1)]. Unfortunately Apple's CMQuaternion is struct and thus you need a small helper class.</p> <pre><code>Quaternion e = [[Quaternion alloc] initWithValues:0 y:0 z:1 w:0]; CMQuaternion cm = deviceMotion.attitude.quaternion; Quaternion quat = [[Quaternion alloc] initWithValues:cm.x y:cm.y z:cm.z w: cm.w]; Quaternion quatConjugate = [[Quaternion alloc] initWithValues:-cm.x y:-cm.y z:-cm.z w: cm.w]; [quat multiplyWithRight:e]; [quat multiplyWithRight:quatConjugate]; // quat.x, .y, .z contain your normal </code></pre> <p>Quaternion.h:</p> <pre><code>@interface Quaternion : NSObject { double w; double x; double y; double z; } @property(readwrite, assign)double w; @property(readwrite, assign)double x; @property(readwrite, assign)double y; @property(readwrite, assign)double z; </code></pre> <p>Quaternion.m:</p> <pre><code>- (Quaternion*) multiplyWithRight:(Quaternion*)q { double newW = w*q.w - x*q.x - y*q.y - z*q.z; double newX = w*q.x + x*q.w + y*q.z - z*q.y; double newY = w*q.y + y*q.w + z*q.x - x*q.z; double newZ = w*q.z + z*q.w + x*q.y - y*q.x; w = newW; x = newX; y = newY; z = newZ; // one multiplication won't denormalise but when multipling again and again // we should assure that the result is normalised return self; } - (id) initWithValues:(double)w2 x:(double)x2 y:(double)y2 z:(double)z2 { if ((self = [super init])) { x = x2; y = y2; z = z2; w = w2; } return self; } </code></pre> <p>I know quaternions are a bit weird at the beginning but once you have got an idea they are really brilliant. It helped me to imagine a quaternion as a rotation around the vector (x, y, z) and w is (cosine of) the angle. </p> <p>If you need to do more with them take a look at <a href="http://code.google.com/p/cocoamath/" rel="nofollow">cocoamath</a> open source project. The classes Quaternion and its extension QuaternionOperations are a good starting point.</p> <p>For the sake of completeness, yes you can do it with matrix multiplication as well:</p> <p><strong>n = M * e</strong></p> <p>But I would prefer the quaternion way it saves you all the trigonometric hassle and performs better.</p>
 

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