【发布时间】:2014-09-16 05:33:16
【问题描述】:
我一直在寻找如何使用设备硬件(指南针、加速度计)旋转 3D 对象
但是,方位角在多个设备上令人难以置信。 (想象一下这是绑在你脸上的,左右看)
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
//Log.i("TAG", "Sensor " + type);
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
MagneticFieldValues_last[0] = event.values[0];
MagneticFieldValues_last[1] = event.values[1];
MagneticFieldValues_last[2] = event.values[2];
bHaveMagneticField = true;
}
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
AccelerometerValues_last[0] = event.values[0];
AccelerometerValues_last[1] = event.values[1];
AccelerometerValues_last[2] = event.values[2];
bHaveAccelerometer = true;
}
if(bHaveMagneticField && bHaveAccelerometer)
{
if(SensorManager.getRotationMatrix(R, null, AccelerometerValues_last, MagneticFieldValues_last))
{
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, remapR);
SensorManager.getOrientation(remapR, orientationValues);
Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
pitch2 = (float) (-Math.atan2(orientationVector[1], orientationVector[2]) * RADIANS_TO_DEGREES);
Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
orientation = (float) (-Math.atan2(orientationVector[0], orientationVector[1]) * RADIANS_TO_DEGREES);
Matrix.invertM(remapR_inv, 0, remapR, 0);
Matrix.multiplyMV(azimuthVector, 0, remapR_inv, 0, sZVector, 0);
azimuth = (float) (180 + Math.atan2(azimuthVector[0], azimuthVector[1]) * RADIANS_TO_DEGREES);
}
}
}
更新
发现这似乎可以解决问题 http://blog.thomnichols.org/2011/08/smoothing-sensor-data-with-a-low-pass-filter
更新 2
遗憾的是它并没有起到作用,仍然需要一种方法来平滑值。其他人是怎么做到的?
【问题讨论】:
标签: android accelerometer android-sensors sensormanager