gpt4 book ai didi

java - Android getOrientation 问题 - 沿轴的镜像值

转载 作者:行者123 更新时间:2023-11-30 11:27:20 25 4
gpt4 key购买 nike

我在使用 getOrientation 时遇到了一些麻烦。我在横向模式下运行我的应用程序,但我在使用一个轴时遇到了问题。代码中有更多解释,一些图像:

enter image description here enter image description here

    valuesMagnet      = new float[3];
valuesAccel = new float[3];
valuesOrientation = new float[3];
rotationMatrix = new float[9];
rotationMatrixTemp= new float[9];

....


@Override
public void onSensorChanged(SensorEvent event) {


switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, valuesAccel, 0, 3);
break;

case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, valuesMagnet, 0, 3);
break;
}

if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) {
//SensorManager.remapCoordinateSystem(rotationMatrixTemp, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix);
SensorManager.getOrientation(rotationMatrix, valuesOrientation);

for (int i = 0; i < valuesOrientation.length; i++) {
valuesOrientation[i] /= Math.PI;
valuesOrientation[i] *= 100;
valuesOrientation[i] = (int)valuesOrientation[i];
valuesOrientation[i] /= 100;
}

updateOrientationBuffer(valuesOrientation);

quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation()));
}
else {
Log.d("App", "Matrix rotate error");
}
}

我希望 orientation[1] 轴在将手机旋转 180 度时给出介于 -1 和 1 之间的全范围值。此外,目标设备没有陀螺仪。

最佳答案

@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, valuesAccel, 0, 3);
break;

case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, valuesMagnet, 0, 3);
break;

case Sensor.TYPE_GRAVITY:
System.arraycopy(event.values, 0, valuesGravity, 0, 3);
break;

default:
break;
}

if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) {
SensorManager.getOrientation(rotationMatrixTemp, valuesOrientation);

if(valuesGravity[2] < 0) {
if (valuesOrientation[1] > 0) {
valuesOrientation[1] = (float) (Math.PI - valuesOrientation[1]);
}
else {
valuesOrientation[1] = (float) (-Math.PI - valuesOrientation[1]);
}
}

for (int i = 0; i < valuesOrientation.length; i++) {
valuesOrientation[i] /= Math.PI;
valuesOrientation[i] *= 100;
valuesOrientation[i] = (int)valuesOrientation[i];
valuesOrientation[i] /= 100;
}


updateOrientationBuffer(valuesOrientation);

quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation()));
}
else {
Log.d("Quadcopter-SM", "Matrix rotate error");
}
}

关于java - Android getOrientation 问题 - 沿轴的镜像值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com