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 8091863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:09:44+00:00 2026-06-05T20:09:44+00:00

How can I get a direction vector representing the direction the back of the

  • 0

How can I get a direction vector representing the direction the back of the device is pointing relative to earth coordinates?

For example, if place down on a desk (screen facing up) it should read [0,0,-1] and if held vertically facing north it should read [1,0,0], etc.

I know how to calculate it from heading, pitch, and roll, as long as those are relative to earth coordinates. To be clear here I am not looking for angular velocity, but the actual current angle relative to the plane tangent to the earth. So if the device is held vertically and facing north, the angle “alpha” should read 0 or 360, the angle “beta” should read 90, and “gamma” should read 0. I can’t figure out how to get these values either.

I’ve been reading the API all day and I still cannot find how to get either of these things.

public void onSensorChanged(SensorEvent event) {
    // ?    
}

Thanks for any insights.

  • 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-05T20:09:45+00:00Added an answer on June 5, 2026 at 8:09 pm

    SensorManager.getRotationMatrix() does what’s outlined below, written before I found this out. I’ll leave the added explanation because if you want to correct for the difference between magnetic and true north you’ll still need it.

    The rough algorithm is to get the rotation matrix, multiply vector [0,0,-1] by it, then adjust this to your coordinate system. Why? Android docs give the coordinate systems for device and world

    deviceworld

    Note [0,0,-1] in Android device coords points perpendicular backward away from the screen. If you multiply rotation matrix R by this vector, you’ll get [0,0,-1] in world coords when the device is on the table facing up as you desire. When it’s upright facing north, you’ll get [0,-1,0], which indicates that you’ve chosen a coordinate system where x and y are swapped with respect to the Android system, but that’s simply a change of conventions.

    Note R * [0,0,-1]^T is just the third column of R negated. From this I get the pseudocode:

    getRotationMatrix(R);
    Let v = first three elements of third column of R.
    swap v[0] and v[1]
    

    This ought to get what you want.

    Additional information on what getRotationMatrix() is doing follows.


    You need both accerometer data to establish the direction “down” and magnetometer data to determine the direction “north.” You’ll have to assume the accelerometers are sensing only gravity (the device is stationary or moving at a steady velocity). Then you need to project the magnetometer vector onto the plane perpendicular to the gravity vector (because the magnetic field is not generally tangent to the earth’s surface). This gives you two axes. The third is orthogonal, so can be computed by cross product. This gives you the earth coordinate vectors in the device system. It looks like you want the inverse: device coordinates in earth coordinates. For this just construct the matrix of direction cosines and invert.

    I will add that the above discussion assumes that the magnetometer vector points north. I think (from high school science!) it’s actually toward magnetic south, but have no device at hand so can’t try it. Of course magnetic north/south is different from true by zero to 180 degrees depending where you are on the earth. You can retrieve GPS coordinates and compute an actual offset.

    If you are not familiar with the math needed to do these, I can explain further, but it will have to be later.

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

Sidebar

Related Questions

my string is: [{property:surname,direction:ASC}] can I get GSON to deserialise this, without adding to
Can anyone point me in the direction of how I could get a NUnit
I have a 3D unity vector pointing in a certain direction. Now I want
I calculated a direction (you can call it a vector if you want, but
Can get all triples with value null in specific field? All people with date_of_birth
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't
I can get the file path with: (message (file-name-directory (or buffer-file-name load-file-name))) But if
I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd
You can get the nodes of the document object of an <iframe> with the
You can get underground processes by ps ux I am searching a way to

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.