24069

Using orientation information to get down direction

Question:

I am working on an app that involves drawing a line on my android screen based on its orientation and could use some help or pointers.

The line is drawn in the following way: If the phone is held flat then then the line shrinks and becomes a dot and as the phone is tilted and orientatated the line becomes bigger - ie the phone stood up and the line points down and is max magnitude of 9.8 and held flat it is a small dot. Crucialy no matter what angle the phone is held at the arrow allways points down- ie the line of gravity.

Now I figured out how to calculate the yaw pitch and roll angles of the phones but mathematically I am a bit lost on how to derive the vector of this line from this information - any pointers would be most welcome.

Thanks

Answer1:

Ok so I figured this out with a lot of help from the Replica Island source and in turn an Nvidia paper.

Once you have the pitch, roll, yaw from the TYPE_ORIENTATION sensor reading:

@Override public void onSensorChanged(SensorEvent event) { synchronized (this) { m_orientationInput[0] = x; m_orientationInput[1] = y; m_orientationInput[2] = z; canonicalOrientationToScreenOrientation(m_rotationIndex, m_orientationInput, m_orientationOutput); // Now we have screen space rotations around xyz. final float horizontalMotion = m_orientationOutput[0] / 90.0f; final float verticalMotion = m_orientationOutput[1] / 90.0f; // send details to renderer.... } }

Here is the canonicalOrientationToScreenOrientation function:

// From NVIDIA http://developer.download.nvidia.com/tegra/docs/tegra_android_accelerometer_v5f.pdf private void canonicalOrientationToScreenOrientation(int displayRotation, float[] canVec, float[] screenVec) { final int axisSwap[][] = { { 1, -1, 0, 1 }, // ROTATION_0 {-1, -1, 1, 0 }, // ROTATION_90 {-1, 1, 0, 1 }, // ROTATION_180 { 1, 1, 1, 0 } // ROTATION_270 }; final int[] as = axisSwap[displayRotation]; screenVec[0] = (float)as[0] * canVec[ as[2] ]; screenVec[1] = (float)as[1] * canVec[ as[3] ]; screenVec[2] = canVec[2]; }

Recommend

  • Can't pass arguments to thread function
  • how to make text align in android listview from right to left?
  • Validate character amount, text length and decimal places from input using javascript
  • Value of “this” is incorrect when debugging Babel transpiled React with Chrome Devtools
  • Calculate gravity/jump in pygame
  • OpenGL camera - View matrix from quaternion behaves incorrectly and pitch is too restricted
  • Android center TextView horizontally while keeping left align
  • Repeat audio clip in Unity, with increasingly smaller intervals
  • .Net Toolstrip/MenuStrip Focus Issues
  • Need Help on the scrolling Bar in Android? [closed]
  • html, body 100% causing scrollbar to appear
  • Google Streetview: Conversion between FOV and Zoom
  • Two gridView with one scroll in android
  • How can I get access to the current selection inside a D3 callback?
  • Call knockout viewmodel function in jQuery
  • Localization using resources file not working
  • Loop over multiple file extensions from bash script
  • gcc treat all warnings as errors EXCEPT X
  • How to change drop down button in combobox control?
  • JQuery Mobile: data-direction=“reverse” not emulating initial transition
  • Multiple Layouts Previews for Android in Eclipse
  • how do you read the ramda docs?
  • Delphi: StringGrid, position and Context Menu
  • Android: make background image changable
  • How to get or calculate size of Azure File/Share or Service
  • IE readonly textarea problem
  • How to model a mixture of finite components from different parametric families with JAGS?
  • EditText ignores the ImeActionLabel
  • Scrolling News Ticker Jquery - Issues
  • SQL Server re-calculate or not?
  • Receive mouse move even cursor is outside control
  • How to disable all widgets inside Panel or inside Composite?
  • SharedPreferences or SQLite Database?
  • jQuery ready not fired after rails link_to is clicked
  • Uncaught Error: Could not find module `ember-load-initializers`
  • swift auto completion not working in Xcode6-Beta
  • How would I use PHP exceptions to define a redirect?
  • Jquery - Jquery Wysiwyg return html as a string
  • Programmatically clearing map cache