1

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

这条线是按以下方式绘制的:如果手机平放,那么这条线会缩小并变成一个点,随着手机倾斜和定向,这条线会变得更大——即手机竖起来,线朝下并且是最大的震级为 9.8 并保持平坦,它是一个小点。至关重要的是,无论手机以什么角度握在箭头处,箭头始终指向下方——即重力线。

现在我想出了如何计算手机的偏航俯仰角和滚动角,但从数学上讲,我对如何从这些信息中推导出这条线的向量有点迷茫——任何指针都会受到欢迎。

谢谢

4

1 回答 1

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]; 
}
于 2011-02-27T20:00:08.923 回答