发现 Sensor.TYPE_MAGNETIC_FIELD 的方位角比 Sensor.TYPE_ORIENTATION 更不稳定
我正在做一些增强现实应用程序,我需要方位角来计算屏幕上的某些对象位置。我尝试使用 Sensor.TYPE_ORIENTATION
获取方向数据,由于它已被弃用,我尝试了 Sensor.TYPE_MAGNETIC_FIELD
。我的问题是,与 Sensor.TYPE_ORIENTATION
相比,使用 Sensor.TYPE_MAGNETIC_FIELD
和 SensorManager.getOrientation
获得的值相当不稳定,这使得我的对象跳跃屏幕上的一点(当不移动值时,值会在一个范围内跳跃,例如[azimuth-5 azimuth+5],而它几乎不会移动传感器.TYPE_ORIENTATION
)。
传感器.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)));
}
}
传感器.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));
}
}
I'm doing some augmented reality app and I need the azimuth to compute some object position on the screen. I tried to get orientation data using Sensor.TYPE_ORIENTATION
and since it's deprecated I tried Sensor.TYPE_MAGNETIC_FIELD
. My problem is that the value obtained with Sensor.TYPE_MAGNETIC_FIELD
and SensorManager.getOrientation
is quite unstable compared to Sensor.TYPE_ORIENTATION
, which makes my object jump a bit on the screen (when not moving the value jump in a range say [azimuth-5 azimuth+5] while it hardly moves with 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));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当使用 SensorManager.DELAY_FASTER 时才会发生这种情况。使用 SensorManager.SENSOR_DELAY_GAME 值更稳定......
It happens only when using SensorManager.DELAY_FASTER. With SensorManager.SENSOR_DELAY_GAME the values are more stable...