How can I use SensorManager.getOrientation for tilt controls like "My Paper Plane"?

richq picture richq · Jan 2, 2011 · Viewed 30.2k times · Source

The Android game My Paper Plane is a great example of how to implement tilt controls, but I've been struggling to understand how I can do something similar.

I have the following example that uses getOrientation() from the SensorManager. The whole thing is on pastebin here. It just prints the orientation values to text fields. Here is the most relevant snippet:

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

The problem is that the values this spits out look like nonsense, or at least there's no way to isolate which type of motion the user performed. I've drawn a diagram to indicate the 2 types of motion that I'd like to detect - 1. "tilt" for pitch and 2. "rotate" for roll/steering:

Figure 1. Pitch and roll

(That's an isometric-ish view of a phone in landscape mode, of course)

When I tilt the phone forwards and backwards along its long axis - shown by 1. - I expected only 1 of the values to change much, but all of them seem to change drastically. Similarly, if I rotate the phone about an imaginary line that comes out of the screen - shown in 2. - I'd hope that only the roll value changes, but all the values change a lot.

The problem is when I calibrate my game - which means recording the current values of the angles x, y and z - I later don't know how to interpret incoming updated angles to say "ok, looks like you have tilted the phone and you want to roll 3 degrees left". It's more like "ok, you've moved the phone and you're a-tiltin' and a-rollin' at the same time", even if the intent was only a roll. Make sense?

Any ideas? I've tried using remapCoordinateSystem to see if changing the axis has any effect. No joy. I think I'm missing something fundamental with this :-(

Answer

Peter Knego picture Peter Knego · Jan 2, 2011

You mixed the accelerator and megnetic sensor arrays. The code should be:

if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                    m_lastAccels, m_lastMagFields)) {

Check out getRotationMatrix(..)