private void updateOrientation() { float[] lastAccelerometer = mSensorData[kSensorType_Accelerometer].values; float[] lastMagnetometer = mSensorData[kSensorType_Magnetometer].values; if (lastAccelerometer != null && lastMagnetometer != null) { SensorManager.getRotationMatrix(mRotation, null, lastAccelerometer, lastMagnetometer); SensorManager.getOrientation(mRotation, mOrientation); final boolean kUse4Components = true; if (kUse4Components) { SensorManager.getQuaternionFromVector(mQuaternion, mOrientation); // Quaternions in Android are stored as [w, x, y, z], so we change it to [x, y, z, w] float w = mQuaternion[0]; mQuaternion[0] = mQuaternion[1]; mQuaternion[1] = mQuaternion[2]; mQuaternion[2] = mQuaternion[3]; mQuaternion[3] = w; mSensorData[kSensorType_Quaternion].values = mQuaternion; } else { mSensorData[kSensorType_Quaternion].values = mOrientation; } } }
switch (event.sensor.getType()) { case Sensor.TYPE_ROTATION_VECTOR: SensorManager.getQuaternionFromVector(latestQuaternion, event.values); if (!haveRotVecData) { initialiseDefaultFilters(SMOOTHING_FACTOR_HIGH_ACC);
@Override public void onSensorChanged(SensorEvent event) { // we received a sensor event. it is a good practice to check // that we received the proper event if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { // convert the rotation-vector to a 4x4 matrix. the matrix // is interpreted by Open GL as the inverse of the // rotation-vector, which is what we want. SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix.matrix, event.values); // Get Quaternion // Calculate angle. Starting with API_18, Android will provide this value as event.values[3], but if not, we have to calculate it manually. SensorManager.getQuaternionFromVector(temporaryQuaternion, event.values); currentOrientationQuaternion.setXYZW(temporaryQuaternion[1], temporaryQuaternion[2], temporaryQuaternion[3], -temporaryQuaternion[0]); } } }
SensorManager.getQuaternionFromVector(temporaryQuaternion, event.values);
SensorManager.getQuaternionFromVector(temporaryQuaternion, event.values);
SensorManager.getQuaternionFromVector(q, event.values);