1. Check out our app, Forums for Android! Download and leave feedback here!

Tilt Sensing

Discussion in 'Application Development' started by Kailas, Aug 30, 2010.

  1. Kailas

    Kailas Member
    Thread Starter
    15

    Aug 25, 2010
    19
    0
    15
    Australia
    Hi everyone.

    I'm currently developing a game in which you move around the level by tilting the phone (similar to labyrinth).

    Does anyone have any experience using SensorManager.getOrientation()? Or does anyone know of anywhere that explains how to use it? Is that even the right way to do it?
     

    Advertisement

  2. SWhite

    SWhite Member
    16

    Apr 15, 2010
    17
    1
    16
    Student, Developer
    Louisiana
    you have the right idea so far.

    try this

    SensorManager sm= Context.getSystemService(SENSOR_SERVICE);
    float[] a= sm.getOrientation();
    float z_axisRotation=a[0];
    float x_axisRotation=a[1];
    float y_axisRotation=a[2];

    havnt tried it but pretty sure this works, either way it should give you an idea
     
  3. Kailas

    Kailas Member
    Thread Starter
    15

    Aug 25, 2010
    19
    0
    15
    Australia
    Heh, I literally got it working about 15 seconds ago. Thanks though.
     
  4. SWhite

    SWhite Member
    16

    Apr 15, 2010
    17
    1
    16
    Student, Developer
    Louisiana
    no problem. was i in the ballpark? i didnt try running it
     
  5. Kailas

    Kailas Member
    Thread Starter
    15

    Aug 25, 2010
    19
    0
    15
    Australia
    You weren't wrong, just missing a lot of code. Here's the relevant parts of my code.

    //Variables
    private SensorManager mSensorManager;
    private float[] mAccelerometerReading;
    private float[] mMagneticFieldReading;
    private float[] mRotationMatrix = new float[16];
    private float[] mRemapedRotationMatrix = new float[16];
    private float[] mOrientation = new float[3];

    //Constructor
    mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(this,
    mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
    SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this,
    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    SensorManager.SENSOR_DELAY_GAME);

    //Sensor reading callback
    public void onSensorChanged(SensorEvent event)
    {
    switch (event.sensor.getType())
    {
    case Sensor.TYPE_ACCELEROMETER:
    {
    mAccelerometerReading = event.values.clone();
    break;
    }
    case Sensor.TYPE_MAGNETIC_FIELD:
    {
    mMagneticFieldReading = event.values.clone();
    break;
    }
    }
    if(mAccelerometerReading != null && mMagneticFieldReading != null &&
    SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelerometerReading, mMagneticFieldReading))
    {
    SensorManager.remapCoordinateSystem(mRotationMatrix,
    SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mRemapedRotationMatrix);
    SensorManager.getOrientation(mRemapedRotationMatrix, mOrientation);
    }
    }

    //Where you want to read the orientation
    if(mOrientation != null)
    {
    //do stuff with mOrientation[0]
    //do stuff with mOrientation[1]
    //do stuff with mOrientation[2]
    //to control a ball i used
    //mOrientation[2] for horizontal movement and
    //- mOrientation[1] for vertical movement
    }
     
  6. SWhite

    SWhite Member
    16

    Apr 15, 2010
    17
    1
    16
    Student, Developer
    Louisiana
    one thing to keep in mind is to keep your variables null for as long as possible in the course of your program. it helps to keep the app from being killed by the system if memory gets tight

    im curious as to why you used the rotation matrix instead of the plain x/y/z rotation values
     
  7. Kailas

    Kailas Member
    Thread Starter
    15

    Aug 25, 2010
    19
    0
    15
    Australia
    I'm not sure what you mean. Which parts of my code would you change to use plain x/y/z values?
     
  8. SWhite

    SWhite Member
    16

    Apr 15, 2010
    17
    1
    16
    Student, Developer
    Louisiana
    before you remap the values from the sensor, the .getOrientation() returns the rotation on each axis as a float value representing the degrees from level.

    the api is foggy as to what new coordinate system it changes the results to after the remap. i was hoping you knew
     
  9. Kailas

    Kailas Member
    Thread Starter
    15

    Aug 25, 2010
    19
    0
    15
    Australia
    getOrientation() takes in the remapped rotation matrix as one of its arguments and uses that to calculate the 3 axis values (it doesn't actually check the phone's orientation, just the orientation of the matrix it is given).

    Taking out the call to remapCoordinateSystem() makes the ball roll up to the left when the phone is flat. I'm not sure what coordinate system the rotation matrix is using before I remap it.
     

Share This Page

Loading...