使用方向信息来降低方向

时间:2011-02-27 03:40:47

标签: android math rotation orientation sensor

我正在制作一个应用程序,它涉及在我的Android屏幕上根据其方向绘制一条线,并可以使用一些帮助或指针。

该线路按以下方式绘制:如果手机保持平坦,那么线路会收缩并变成一个点,当手机倾斜并定向时,线路会变大 - 即手机站起来,线路指向下方并且最大幅度为9.8并且保持平坦它是一个小点。 Crucialy无论在什么角度下,手机都被箭头指向下方,即重力线。

现在我想出了如何计算手机的偏航俯仰角和滚动角,但从数学角度来看,我对如何从这些信息中推导出该线的矢量感到有点迷失 - 任何指针都是最受欢迎的。

由于

1 个答案:

答案 0 :(得分:2)

好的,所以我在Replica Island的帮助下得到了很多帮助,反过来又是一篇Nvidia论文。

从TYPE_ORIENTATION传感器读取音高,滚动,偏航:

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

   }
}

这是canonicalOrientationToScreenOrientation函数:

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