Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:07:54+00:00 2026-06-04T22:07:54+00:00

I would like to use CMAttitude to know the vector normal to the glass

  • 0

I would like to use CMAttitude to know the vector normal to the glass of the iPad/iPhone’s screen (relative to the ground). As such, I would get vectors like the following:

enter image description here

Notice that this is different from orientation, in that I don’t care how the device is rotated about the z axis. So if I was holding the iPad above my head facing down, it would read (0,-1,0), and even as I spun it around above my head (like a helicopter), it would continue to read (0,-1,0):

enter image description here

I feel like this might be pretty easy, but as I am new to quaternions and don’t fully understand the reference frame options for device motion, its been evading me all day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:07:56+00:00Added an answer on June 4, 2026 at 10:07 pm
    1. 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)
    2. CMAttitude which you can get via
      CMMotionManager.deviceMotion provides the rotation
      relative to a reference frame. Its properties quaternion, roation
      matrix and Euler angles are just different representations.
    3. The reference frame can be specified when you start device motion updates using CMMotionManager’s startDeviceMotionUpdatesUsingReferenceFrame method. Until iOS 4 you had to use multiplyByInverseOfAttitude

    Putting this together you just have to multiply the quaternion in the right way with the normal vector when the device lies face up on the table. Now we need this right way of quaternion multiplication that represents a rotation: According to Rotating vectors this is done by:

    n = q * e * q’ where q is the quaternion delivered by CMAttitude [w, (x, y, z)], q’ is its conjugate [w, (-x, -y, -z)] and e 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.

    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
    

    Quaternion.h:

    @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;
    

    Quaternion.m:

    - (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;
    }
    

    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.

    If you need to do more with them take a look at cocoamath open source project. The classes Quaternion and its extension QuaternionOperations are a good starting point.

    For the sake of completeness, yes you can do it with matrix multiplication as well:

    n = M * e

    But I would prefer the quaternion way it saves you all the trigonometric hassle and performs better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use Maven's password encryption such as it uses for nodes
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I
I would like to use a pre-commit hook in riak to validate the json
I would like to use GIO (which is part of GLIB) on Android by
I would like to use the MFMailComposeViewController mailComposeDelegate property with completion block syntax, but
I would like to use the following class across multiple modules without needing log

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.