从地板计算相机角度
calculating camera angle from floor
我正在尝试从地板计算摄像机角度。我已将设备放在地板上以计算角度。我正在使用以下代码来计算角度。
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == 1) {
float[] values = event.values;
float f3 = this.pastX * 0.925F + values[0] * 0.07499999F;
float f2 = this.pastY * 0.925F + values[1] * 0.07499999F;
float f1 = this.pastZ * 0.925F + values[2] * 0.07499999F;
this.pastX = f3;
this.pastY = f2;
this.pastZ = f1;
applyRotationMatrix(this.rotationMatrix, f3, f2, f1, this.gravitySensorCorrected);
double d = computeAngleOfPhoneFromFloorInDegrees(this.gravitySensorCorrected);
}
}
以下是我用来应用旋转矩阵的代码,
private static void applyRotationMatrix(float[] rotationMatrix, float f3, float f2, float f1, float[] correctedOp) {
correctedOp[0] = (rotationMatrix[0] * f3 + rotationMatrix[1] * f2 + rotationMatrix[2] * f1);
correctedOp[1] = (rotationMatrix[3] * f3 + rotationMatrix[4] * f2 + rotationMatrix[5] * f1);
correctedOp[2] = (rotationMatrix[6] * f3 + rotationMatrix[7] * f2 + rotationMatrix[8] * f1);
}
然后我使用下面的代码来计算角度
private static double computeAngleOfPhoneFromFloorInDegrees(float[] floatArray) {
float f3 = floatArray[0];
float f1 = floatArray[1];
float f2 = floatArray[2];
double d1 = Math.sqrt(f3 * f3 + f1 * f1 + f2 * f2);
double d2 = f1 / d1;
d1 = f2 / d1;
d1 = Math.acos(d1 / Math.sqrt(d2 * d2 + d1 * d1));
if (d2 > 0.0D) {
return 180.0D * d1 / 3.141592653589793D;
}
}
但是这段代码并不总是有效,有时会提供错误的角度。如何计算从地板到摄像机的角度?它打印 NaN
- 如果
d2 < 0.0D
,你不会return
- 我认为代码应该只是
asin(f1 / d1)
,你的计算过于复杂了。
我正在尝试从地板计算摄像机角度。我已将设备放在地板上以计算角度。我正在使用以下代码来计算角度。
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == 1) {
float[] values = event.values;
float f3 = this.pastX * 0.925F + values[0] * 0.07499999F;
float f2 = this.pastY * 0.925F + values[1] * 0.07499999F;
float f1 = this.pastZ * 0.925F + values[2] * 0.07499999F;
this.pastX = f3;
this.pastY = f2;
this.pastZ = f1;
applyRotationMatrix(this.rotationMatrix, f3, f2, f1, this.gravitySensorCorrected);
double d = computeAngleOfPhoneFromFloorInDegrees(this.gravitySensorCorrected);
}
}
以下是我用来应用旋转矩阵的代码,
private static void applyRotationMatrix(float[] rotationMatrix, float f3, float f2, float f1, float[] correctedOp) {
correctedOp[0] = (rotationMatrix[0] * f3 + rotationMatrix[1] * f2 + rotationMatrix[2] * f1);
correctedOp[1] = (rotationMatrix[3] * f3 + rotationMatrix[4] * f2 + rotationMatrix[5] * f1);
correctedOp[2] = (rotationMatrix[6] * f3 + rotationMatrix[7] * f2 + rotationMatrix[8] * f1);
}
然后我使用下面的代码来计算角度
private static double computeAngleOfPhoneFromFloorInDegrees(float[] floatArray) {
float f3 = floatArray[0];
float f1 = floatArray[1];
float f2 = floatArray[2];
double d1 = Math.sqrt(f3 * f3 + f1 * f1 + f2 * f2);
double d2 = f1 / d1;
d1 = f2 / d1;
d1 = Math.acos(d1 / Math.sqrt(d2 * d2 + d1 * d1));
if (d2 > 0.0D) {
return 180.0D * d1 / 3.141592653589793D;
}
}
但是这段代码并不总是有效,有时会提供错误的角度。如何计算从地板到摄像机的角度?它打印 NaN
- 如果
d2 < 0.0D
,你不会return
- 我认为代码应该只是
asin(f1 / d1)
,你的计算过于复杂了。