• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Tilt Sensing

Kailas

Newbie
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?
 
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
 
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
}
 
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
 
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
 
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.
 
Back
Top Bottom