Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:12:38+00:00 2026-06-07T01:12:38+00:00

In my 2d game I have the following code which is responsible for game

  • 0

In my 2d game I have the following code which is responsible for game entity control (flying plane). It all seems to be working fine when it comes for phones, but unfortunately I’ve getting some information that on Android tablets the steering is completely unreliable (axis are messed up, or it doesn’t work at all). Unfortunately I don’t have a tablet of my own, so I cannot investigate it closer. So.. what’s wrong with the following code? (for the clarity I put only the code related to sensors)

// ...
private float[] accelerometerValues;
private float[] magneticFieldValues;
private float[] R;
private float[] I;
private float[] outR;
private float[] sensorValues;

private Sensor accelerometer;
private Sensor magneticField;
// ...

// ... sensor initialization
    sensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
    if(sensorManager == null)
        return;
    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
// ...

// ... onPause() sensor is being unregistered
public void onResume() {
    if(!sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_GAME) ||
       !sensorManager.registerListener(sensorListener, magneticField, SensorManager.SENSOR_DELAY_GAME))
// ...

    // ...
    sensorListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor arg0, int arg1) {              
        }
        public void onSensorChanged(android.hardware.SensorEvent event) {
            synchronized(InputMgr.this) {
                switch(event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    System.arraycopy(event.values, 0, accelerometerValues, 0, 3);
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    System.arraycopy(event.values, 0, magneticFieldValues, 0, 3);
                    break;
                }
            }
        }
    };  
    // ...  

    // used somewhere in the game
public void getSensorValues(float values[]) {
    synchronized(InputMgr.this) {
        SensorManager.getRotationMatrix(R, I, accelerometerValues, magneticFieldValues);
        SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, outR);
        SensorManager.getOrientation(outR, sensorValues);
        System.arraycopy(sensorValues, 0, values, 0, sensorValues.length);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:12:40+00:00Added an answer on June 7, 2026 at 1:12 am

    On devices whose default orientation is landscape (-> most tablets), the sensor values are kind of ‘wrong’ (I don’t know why). So you need to catch those devices and remap your Rotation Matrix.

    To check whether the matrix needs to be remapped, you can use this code:

    public boolean needToRemapOrientationMatrix;
    
    // compute once (e.g. in onCreate() of your Activity):
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int orientation;
    if(display.getWidth() < display.getHeight()) orientation = Configuration.ORIENTATION_PORTRAIT;
    else if(display.getWidth() > display.getHeight()) orientation = Configuration.ORIENTATION_LANDSCAPE;
    else orientation = Configuration.ORIENTATION_SQUARE;
    int rotation = display.getRotation();
    needToRemapOrientationMatrix =
        (orientation==Configuration.ORIENTATION_LANDSCAPE && (rotation==Surface.ROTATION_0 || rotation==Surface.ROTATION_180)) ||
        (orientation==Configuration.ORIENTATION_PORTRAIT && (rotation==Surface.ROTATION_90 || rotation==Surface.ROTATION_270));
    

    And when you read the sensor values, remap the matrix if needed:

    public void getSensorValues(float values[]) {
        synchronized(InputMgr.this) {
            SensorManager.getRotationMatrix(R, I, accelerometerValues, magneticFieldValues);
    
            if(needToRemapOrientationMatrix)
                SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, R);
    
            SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, outR);
            SensorManager.getOrientation(outR, sensorValues);
            System.arraycopy(sensorValues, 0, values, 0, sensorValues.length);
        }
    }
    

    This worked for me, I hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a console game in which I have the following pieces of
I have the following segment of code in which I want to Group everything
I have a two year old game code which was completely written in cpp
I'm working on a little Java game in which all sorts of events can
I have the following code to create an NSTimer which should update a label
I have the following scenarios: Scenario: Create a game with valid information Given I
I have a class with the following declaration of the fields: public class Game
I have written a game (I'll give it the name Gamer) in which I
I draw buildings in my game world, i shade them with the following code:
I have the following code: procedure TCellBlock.GeneratePtoQ; var x,y: integer; i: integer; Change: cardinal;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.