Tilt Sensing


Last Updated:

  1. Kailas

    Kailas Member This Topic's Starter

    Joined:
    Aug 25, 2010
    Messages:
    19
    Likes Received:
    0
    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

    Joined:
    Apr 15, 2010
    Messages:
    17
    Likes Received:
    1
    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 This Topic's Starter

    Joined:
    Aug 25, 2010
    Messages:
    19
    Likes Received:
    0
    Heh, I literally got it working about 15 seconds ago. Thanks though.
     
  4. SWhite

    SWhite Member

    Joined:
    Apr 15, 2010
    Messages:
    17
    Likes Received:
    1
    no problem. was i in the ballpark? i didnt try running it
     
  5. Kailas

    Kailas Member This Topic's Starter

    Joined:
    Aug 25, 2010
    Messages:
    19
    Likes Received:
    0
    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

    Joined:
    Apr 15, 2010
    Messages:
    17
    Likes Received:
    1
    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 This Topic's Starter

    Joined:
    Aug 25, 2010
    Messages:
    19
    Likes Received:
    0
    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

    Joined:
    Apr 15, 2010
    Messages:
    17
    Likes Received:
    1
    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 This Topic's Starter

    Joined:
    Aug 25, 2010
    Messages:
    19
    Likes Received:
    0
    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...