Screen rotation in degrees

edited January 2014 in Android Mode

Hi all,

Im playing around now for hours to find a way to calculate the screen rotation of a mobile phone from the acceleration sensor data. I managed to get many measures, coordinates etc. but no way to calculate a stable way to access the screen rotation in degrees (0-360).
In the end this should lead to an app that shows some kind of water level that should stay horizontal even if you rotate the phone when holding it in your hand (so that it is not really vertical but some kind of tilted) This shouldn't be so difficult as this information is used to find out the screen orientation (LANDSCAPE/PORTRAIT) ...

Does anyone have some hints for me?

Thank you! Christian from Austria

Answers

  • Ok, maybe this was way too simple for this forum ;) Some hours later I found a solution myself, just by using the atan2-function. See below my code based on the ketai acceleration example.

    import ketai.sensors.*;
    KetaiSensor sensor;
    float accelerometerX, accelerometerY, accelerometerZ,rotation;
    
    void setup()
    {
      sensor = new KetaiSensor(this);
      sensor.start();
      orientation(LANDSCAPE);
    }
    
    void draw()
    {
      background(78, 93, 75);
      noStroke();
       fill(44,44,100);
      rotation=-atan2(accelerometerY,accelerometerX);
      translate(width/2,height/2);
      rotate(rotation);
      rect(-width*1.5,0,width*3,height); 
    }
    
    void onAccelerometerEvent(float x, float y, float z)
    {
      accelerometerX = x;
      accelerometerY = y;
      accelerometerZ = z;
    }
    
Sign In or Register to comment.