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

1

25

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 :-(

Contredanse answered 2/1, 2011 at 0:21 Comment(0)
V
22

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(..)

Verne answered 2/1, 2011 at 1:54 Comment(3)
Well, that was embarrassing. Also: A BIG, FAT THANK YOU! I don't know how many hours I've wasted over the last few days and weeks obsessing over this. Yes, using the API as it was intended makes things work a lot better.Contredanse
I know the feeling ;) For this kind of problems a second pair of eyes is really helpful.Verne
I see that this post seems to be answered, and is a bit old, but I've done a writeup of essentially what you're looking for here: http://blog.samsandberg.com/2015/05/10/tilt-for-android/ - hope it helps someone!Deepset

© 2022 - 2024 — McMap. All rights reserved.