我正在做一些增强现实应用程序,我需要方位角来计算屏幕上的一些对象位置。我尝试使用Sensor.TYPE_ORIENTATION
获取方向数据,因为它已被弃用,我尝试了Sensor.TYPE_MAGNETIC_FIELD
。我的问题是Sensor.TYPE_MAGNETIC_FIELD
和SensorManager.getOrientation
获得的值与Sensor.TYPE_ORIENTATION
相比非常不稳定,这会使我的对象在屏幕上跳转一点(当不在一个范围内移动值跳转时)说 [azimuth-5 azimuth + 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));
}
}
答案 0 :(得分:2)
仅在使用SensorManager.DELAY_FASTER时才会发生。使用SensorManager.SENSOR_DELAY_GAME,值更稳定......