如何将SensorManager.getOrientation用于"My Paper Plane"等倾斜控件?

ric*_*chq 24 android screen-orientation tilt

Android游戏My Paper Plane是如何实现倾斜控制的一个很好的例子,但我一直在努力理解如何做类似的事情.

我有以下示例,它使用SensorManager中的getOrientation().整个事情都在这里的pastebin上.它只是将方向值打印到文本字段.这是最相关的片段:

private void computeOrientation() {
    if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                        m_lastMagFields, m_lastAccels)) {
        SensorManager.getOrientation(m_rotationMatrix, m_orientation);

        /* 1 radian = 57.2957795 degrees */
        /* [0] : yaw, rotation around z axis
         * [1] : pitch, rotation around x axis
         * [2] : roll, rotation around y axis */
        float yaw = m_orientation[0] * 57.2957795f;
        float pitch = m_orientation[1] * 57.2957795f;
        float roll = m_orientation[2] * 57.2957795f;

        /* append returns an average of the last 10 values */
        m_lastYaw = m_filters[0].append(yaw);
        m_lastPitch = m_filters[1].append(pitch);
        m_lastRoll = m_filters[2].append(roll);
        TextView rt = (TextView) findViewById(R.id.roll);
        TextView pt = (TextView) findViewById(R.id.pitch);
        TextView yt = (TextView) findViewById(R.id.yaw);
        yt.setText("azi z: " + m_lastYaw);
        pt.setText("pitch x: " + m_lastPitch);
        rt.setText("roll y: " + m_lastRoll);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这个吐出的值看起来像废话,或者至少没有办法隔离用户执行的运动类型.我已经绘制了一个图表来指示我想要检测的两种类型的运动 - 1."倾斜"用于俯仰和2."旋转"用于滚动/转向:

图1.俯仰和滚动

(当然,这是横向模式下手机的等距视图)

当我沿着长轴向前和向后倾斜手机时 - 显示为1. - 我预计只有1个值会发生很大变化,但所有这些值似乎都会发生巨大变化.类似地,如果我围绕从屏幕出来的假想线旋转手机 - 如图2所示. - 我希望只有滚动值改变,但所有值都会发生很大变化.

问题是当我校准我的游戏 - 这意味着记录角度x,y和z的当前值 - 我后来不知道如何解释传入的更新角度说"好吧,看起来你已经倾斜手机和你想要向左倾3度".它更像是"好吧,你已经移动了手机而且你同时也是'倾斜'和a-rollin'",即使意图只是一个滚动.合理?

有任何想法吗?我尝试使用remapCoordinateSystem来查看更改轴是否有任何影响.没有快乐.我想我错过了一些基本的东西:-(

Pet*_*ego 22

你混合了加速器和磁性传感器阵列.代码应该是:

if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                    m_lastAccels, m_lastMagFields)) {
Run Code Online (Sandbox Code Playgroud)

查看 getRotationMatrix(..)