Rhys
Rhys

Reputation: 2877

How to swtich Accelerometer Axis based on Device?

My android application uses the Y Axis accelerometer to control the character in the game.

This is working fine on most mobile phones, however on my Galaxy Tab 2 10.1, the axis seems to be reversed and I have to manualy change the input for it work.

I would like to write a simple conditional statement that would swap based on device type, however I am unsure what the best practice for this would be. As I am sure someone would have come accross this issue before I ask the question.

How can I determine if the device is a table or a mobile in order to change Accelerometer axis?

Upvotes: 1

Views: 891

Answers (2)

Stochastically
Stochastically

Reputation: 7846

I think this relates to the default orientation of the device, which tends to be portrait for phones, or landscape for tablets. There's a question here about exactly that: How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout) , and based on my experiences I'd say that this is the best answer . An important point to understand is that whatever the display is doing (landscape, portrait, etc), the x-y-z axes used by the sensors don't change.

Upvotes: 3

Rhys
Rhys

Reputation: 2877

I found out the best way to do this is to check the screen size on the device and base a boolean conditional to device on which axis to use. This is tested and working great.

// Check the screen layout to determine if the device is a tablet.

public boolean isTablet(Context context) {
    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    return (xlarge || large);
}

// Changle Accelx or Accely input based on the above (Im using OpenGLES 1.1)

if (isTablet(glGame)) {
    world.update(deltaTime, game.getInput().getAccelX());
}
if (!isTablet(glGame)) {
    world.update(deltaTime, game.getInput().getAccelY());
}

Upvotes: 0

Related Questions