Using orientation information to get down direction

不问归期 提交于 2019-12-24 17:13:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/5131292/using-orientation-information-to-get-down-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!