【问题标题】:How to get degrees by Accelerometer data (Java libGDX)如何通过加速度计数据获取度数(Java libGDX)
【发布时间】:2018-03-24 04:52:36
【问题描述】:

如何从加速度计的值中获取度数?我在 Android Studio 中使用 libGDX 和 Java 代码。 我有一个精灵动画,它直走。视角与顶部正交,我想在倾斜智能手机时旋转精灵。

我怎样才能在屏幕上获得 360° 度,例如像指南针一样,它只是指向北方,它应该指向智能手机倾斜的方向。加速度计传感器怎么可能?或者我还有什么其他可能性?

对不起我的英语

【问题讨论】:

  • 如果您只想获得以度为单位的角度数,应该可以获取加速度计的旋转矩阵并将其转换为欧拉角。我做过一次,但我忘记了我的代码现在在哪里。
  • 我的回答对你有帮助吗?
  • 太棒了!请采纳我的回答,谢谢。祝你好运@Domooo93
  • 我如何接受答案?我不经常使用这个论坛

标签: java android libgdx accelerometer smartphone


【解决方案1】:

一个简单的方法是使用 SensorManager 并实现 SensorEventListener。基本思想是您使用 SensorManager 注册方向传感器,然后在使用 SensorEventListener 实现的 onSensorChanged 委托方法中响应设备方向的变化。确保你取消注册监听器 onPause() 否则它会耗尽你的电池。

作为一个高级示例:

public class SensorActivity extends Activity implements SensorEventListener {

private Sensor mOrientationSensor;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    mOrientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}

@Override
public void onResume() {
    super.onResume();

    if (mSensorManager != null) {
        mSensorManager.registerListener(this, mOrientationSensor, SensorManager.SENSOR_DELAY_UI);
    }

}

@Override
public void onPause() {
    super.onPause();

    mSensorManager.unregisterListener(this, mOrientationSensor);
}


@Override
public void onSensorChanged(SensorEvent event) {
        float degree = Math.round(event.values[0]);

        // do something here
}

注意:方向传感器已被弃用,尽管我仍然认为它效果最好。如果您想尝试,更新的方法如下。

来自安卓文档:https://developer.android.com/guide/topics/sensors/sensors_position.html#sensors-pos-orient

    public class SensorActivity extends Activity implements SensorEventListener {

  private SensorManager mSensorManager;
  private final float[] mAccelerometerReading = new float[3];
  private final float[] mMagnetometerReading = new float[3];

  private final float[] mRotationMatrix = new float[9];
  private final float[] mOrientationAngles = new float[3];

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
    // You must implement this callback in your code.
  }

  @Override
  protected void onResume() {
    super.onResume();

    // Get updates from the accelerometer and magnetometer at a constant rate.
    // To make batch operations more efficient and reduce power consumption,
    // provide support for delaying updates to the application.
    //
    // In this example, the sensor reporting delay is small enough such that
    // the application receives an update before the system checks the sensor
    // readings again.
    mSensorManager.registerListener(this, Sensor.TYPE_ACCELEROMETER,
      SensorManager.SENSOR_DELAY_NORMAL, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, Sensor.TYPE_MAGNETIC_FIELD,
      SensorManager.SENSOR_DELAY_NORMAL, SensorManager.SENSOR_DELAY_UI);
  }

  @Override
  protected void onPause() {
    super.onPause();

    // Don't receive any more updates from either sensor.
    mSensorManager.unregisterListener(this);
  }

  // Get readings from accelerometer and magnetometer. To simplify calculations,
  // consider storing these readings as unit vectors.
  @Override
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor == Sensor.TYPE_ACCELEROMETER) {
      System.arraycopy(event.values, 0, mAccelerometerReading,
        0, mAccelerometerReading.length);
    }
    else if (event.sensor == Sensor.TYPE_MAGNETIC_FIELD) {
      System.arraycopy(event.values, 0, mMagnetometerReading,
        0, mMagnetometerReading.length);
    }
  }

  // Compute the three orientation angles based on the most recent readings from
  // the device's accelerometer and magnetometer.
  public void updateOrientationAngles() {
    // Update rotation matrix, which is needed to update orientation angles.
    mSensorManager.getRotationMatrix(mRotationMatrix, null,
      mAccelerometerReading, mMagnetometerReading);

    // "mRotationMatrix" now has up-to-date information.

    mSensorManager.getOrientation(mRotationMatrix, mOrientationAngles);

    // "mOrientationAngles" now has up-to-date information.
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多