我一直在寻找如何使用设备硬件(指南针、加速度计)旋转 3D 对象
以下指南很棒: http://ibuzzlog.blogspot.co.uk/2012/08/how-to-use-android-sensors.html?showComment= 1409868331799#c665801926070707020
然而,方位角在多个设备上是令人难以置信的跳跃。(想象一下这是绑在你脸上的,左右看)
@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
可悲的是,它没有起到作用,仍然需要一种方法来平滑值。其他人如何做到这一点?