android – 使用Sensor.TYPE_MAGNETIC_FIELD找到的Azimuth比Sensor.TYPE_ORIENTATION更不稳定

我正在做一些增强现实应用程序,我需要方位角来计算屏幕上的一些对象位置.我尝试使用Sensor.TYPE_ORIENTATION获取方向数据,因为它已弃用,我尝试了Sensor.TYPE_MAGNETIC_FIELD.我的问题是,与Sensor.TYPE_ORIENTATION相比,使用Sensor.TYPE_MAGNETIC_FIELD和SensorManager.getOrientation获得的值非常不稳定,这使得我的对象在屏幕上稍微跳了一下(当没有在一个范围内移动值跳转时说[方位角-5方位角] 5]虽然它几乎不随Sensor.TYPE_ORIENTATION而移动.

Sensor.TYPE_ORIENTATION

public void onSensorChanged(SensorEvent event)
{
    if(event.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
        newAzimuth = (event.values[0] + 360)%360;            
        azimuth = (float) ((newAzimuth * kFilteringFactor) + (azimuth * (1.0 - kFilteringFactor)));       
    }
}

Sensor.TYPE_MAGNETIC_FIELD

public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
    }
    if (mags != null && accels != null) {
        SensorManager.getRotationMatrix(R, I, accels, mags);
        SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X,
                SensorManager.AXIS_Z, remappedR);
        SensorManager.getOrientation(remappedR, orientation);
        newAzimuth = (float) Math.round(Math.toDegrees(orientation[0]));
        newAzimuth = (newAzimuth + 360)%360;
        azimuth = (float) (newAzimuth * kFilteringFactor) + (azimuth * (1.0f - kFilteringFactor));
    }
}

最佳答案 仅在使用SensorManager.DELAY_FASTER时才会发生.使用SensorManager.SENSOR_DELAY_GAME,值更稳定……

点赞