1
votes

I'm trying to rotate a model (terrain) situated at (X,Y,Z)=(0,0,0) of my OpenGL-World. +X is east, -Z is north, +Y is altitude. View is looking to -Y because the device Euler angles are (0°,0°,0°) while the device is lying on the table pointing north.

The Problem:

I need to switch the axes of my device to change the measured angle device -y to device z.

Currently rotation around the device x-axis works (results in rotation around World X-axis).

But rotation around device y leads to a rotation around World Y instead of World -Z and rotation around device z to rotation around World Z instead of World Y.

By now I'm running out of ideas how to solve this. Anybody who could give me a hint please?

    (OpenGL)                         (Device)

   ^ +Y-axis                       ^ +z-axis                                  
   *                               *                                 
   *                               *                                 
   *                               *    ^ +y-axis (North)               
   *                               *   *                               
   *                               *  *                                
   *                               * *                                 
   ************> + X-axis          ************> +x-axis                      
  *                                                                   
 *                                                                    
v +Z-axis (Minus North)    

What I've tried so far:

  • Using Euler angles from SensorManager.getOrientation and switching angles works fine, though I get into gimbal lock close to pitch 90 deg. So I'm in search for another solution (the SensorManager rotation matrix or quaternions).
  • SensorManager.remapCoordinateSystem in almost every possible constellation -> doesn't help
  • Changing cols/rows of my rotation matrix from SensorManager.getRotationMatrix -> doesn't help
1

1 Answers

1
votes

Well, I've found a pretty easy solution using quaternions. The axes are beeing switched at the end.

See also:

Now I'm still wondering how to enable pitch angles above 90 deg (in portrait mode). Any ideas?

Activity:

private SensorManager sensorManager;
private Sensor rotationSensor;

public void onCreate(Bundle savedInstanceState) {
  ...
  rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
  ...
}

// Register/unregister SensorManager Listener at onResume()/onPause() !

public void onSensorChanged(SensorEvent event) {

  if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
    // Set rotation vector
    MyRenderer.setOrientationVector(event.values);
  }

}

MyRenderer:

float[] orientationVector = new float[3];

public void setOrientationVector (float[] vector) {

  // Rotation vector to quaternion
  float[] quat = new float[4];
  SensorManager.getQuaternionFromVector(quat, vector);
  // Switch quaternion from [w,x,y,z] to [x,y,z,w]
  float[] switchedQuat = new float[] {quat[1], quat[2], quat[3], quat[0]};

  // Quaternion to rotation matrix
  float[] rotationMatrix = new float[16];
  SensorManager.getRotationMatrixFromVector(rotationMatrix, switchedQuat);

  // Rotation matrix to orientation vector
  SensorManager.getOrientation(rotationMatrix, orientationVector);

}

public void onDrawFrame(GL10 unused) {
  ...
  // Rotate model matrix (note the axes beeing switched!)
  Matrix.rotateM(modelMatrix, 0,
    (float) (orientationVector[1] * 180/Math.PI), 1, 0, 0);

  Matrix.rotateM(modelMatrix, 0,
    (float) (orientationVector[0] * 180/Math.PI), 0, 1, 0);

  Matrix.rotateM(modelMatrix, 0,
    (float) (orientationVector[2] * 180/Math.PI), 0, 0, 1);
  ...
}