我正在尝试在纵向放置设备时进行俯仰和滚动。 轴看起来像这样,它将分别关于 x 和 z 轴。现在我正在使用 SensorManager API 来获取设备平放的俯仰、滚动和偏航,这是默认设置。
当我尝试将平面设备的旋转值转换为垂直方向时,我遇到了其他 SO 用户所说的万向节锁定,这是欧拉角工作方式固有的问题。问题是我已经尝试实现旋转矩阵,因为其他用户必须解决类似的问题,但即使我仍然遇到相同的万向节锁定问题。我已经包含了我的 onSensorChanged 方法,希望有人可以帮助找出问题所在。
public void onSensorChanged(SensorEvent se) {
float degPitch = 0;
float degRoll = 0;
float degYaw = 0;
float radPitch = 0;
float radRoll = 0;
float radYaw = 0;
if (se.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mAccelerometerResult = se.values;
Log.d("onSensorChanged", "Accelerometer: " + mAccelerometerResult.length);
}
if (se.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mMagneticFieldResult = se.values;
Log.d("onSensorChanged", "Magnetic Field: " + mMagneticFieldResult.length);
}
if (mAccelerometerResult != null && mMagneticFieldResult != null) {
float[] rotation = new float[9];
float[] inclination = new float[9];
boolean rotationMatrixCheck = mSensorManager.getRotationMatrix(rotation, inclination, mAccelerometerResult, mMagneticFieldResult);
if (rotationMatrixCheck) {
float[] orientation = new float[3];
mSensorManager.getOrientation(rotation, orientation);
radYaw = orientation[0]; //Yaw = Z axis
radPitch = orientation[1]; //Pitch = X axis
radRoll = orientation[2]; //Roll = Y axis
degYaw = round((float) Math.toDegrees(radYaw), 2);
degPitch = round((float)Math.toDegrees(radPitch), 2);
degRoll = round((float)Math.toDegrees(radRoll), 2);
if ((counter % 10) == 0) {
//mYawTextView.setText(degYaw + "°");
mPitchTextView.setText(degPitch + "°");
mRollTextView.setText(degRoll + "°");
counter = 0;
} else {
counter++;
}
}
}
此外,我什至不确定我是否了解我正在寻找的旋转值,如果我能在纵向轴上获得良好的旋转。如果我想要纵向的设备滚动(大约从我的原始图像的 z 轴),那仍然是平放的设备的滚动(大约来自平轴图像的 y)?
任何可以在这里分享的见解将不胜感激。