发现 Sensor.TYPE_MAGNETIC_FIELD 的方位角比 Sensor.TYPE_ORIENTATION 更不稳定

发布于 2024-11-19 19:21:21 字数 1652 浏览 1 评论 0原文

我正在做一些增强现实应用程序,我需要方位角来计算屏幕上的某些对象位置。我尝试使用 Sensor.TYPE_ORIENTATION 获取方向数据,由于它已被弃用,我尝试了 Sensor.TYPE_MAGNETIC_FIELD。我的问题是,与 Sensor.TYPE_ORIENTATION 相比,使用 Sensor.TYPE_MAGNETIC_FIELDSensorManager.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2024-11-26 19:21:21

仅当使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文