androidaccelerometerplanetilt

How to measure the tilt of the phone in XY plane using accelerometer in Android


I tried to use the Z axis data from SensorEvent.values, but it doesn't detect rotation of my phone in the XY plane, ie. around the Z-axis.

I am using this as a reference for the co-ordinate axes. Is it correct?

axes

How do I measure that motion using accelerometer values?

These games do something similar: Extreme Skater, Doodle Jump.

PS: my phone orientation will be landscape.


Solution

  • Essentially, there is 2 cases here: the device is laying flat and not flat. Flat here means the angle between the surface of the device screen and the world xy plane (I call it the inclination) is less than 25 degree or larger than 155 degree. Think of the phone lying flat or tilt up just a little bit from a table.

    First you need to normalize the accelerometer vector.
    That is if g is the vector returns by the accelerometer sensor event values. In code

    float[] g = new float[3]; 
    g = event.values.clone();
    
    double norm_Of_g = Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);
    
    // Normalize the accelerometer vector
    g[0] = g[0] / norm_Of_g
    g[1] = g[1] / norm_Of_g
    g[2] = g[2] / norm_Of_g
    

    Then the inclination can be calculated as

    int inclination = (int) Math.round(Math.toDegrees(Math.acos(g[2])));
    

    Thus

    if (inclination < 25 || inclination > 155)
    {
        // device is flat
    }
    else
    {
        // device is not flat
    }
    

    For the case of laying flat, you have to use a compass to see how much the device is rotating from the starting position.

    For the case of not flat, the rotation (tilt) is calculated as follow

    int rotation = (int) Math.round(Math.toDegrees(Math.atan2(g[0], g[1])));
    

    Now rotation = 0 means the device is in normal position. That is portrait without any tilt for most phone and probably landscape for tablet. So if you hold a phone as in your picture above and start rotating, the rotation will change and when the phone is in landscape the rotation will be 90 or -90 depends on the direction of rotation.