简体   繁体   English

如何通过加速度计数据获取学位(Java libGDX)

[英]How to get degrees by Accelerometer data (Java libGDX)

how can I get degrees from the values of the Accelerometer ? 如何从加速度计的值中获取度数? I'm useing libGDX and code in Java with Android Studio. 我在Android Studio中使用libGDX和Java代码。 I ve got a sprite animation, which walks straight. 我有一个精灵动画,它可以直走。 The point of View is orthogonal from top, and I want to rotate the sprite when I tilt the smartphone. 视点从顶部是正交的,倾斜智能手机时我想旋转精灵。

How can I get the 360° degrees on the screen, for example like a compass just instead that it points to north it should point to the direction where the smartphone is tilted. 我如何在屏幕上获得360度,例如像指南针那样,它只是指向北,它应该指向智能手机倾斜的方向。 How is it possible with the Accelerometer Sensor ? 加速度传感器怎么办? Or what other possibility do I have ? 或者我还有什么其他可能性?

Sorry for my English 对不起我的英语不好

A simple way of doing this is to use a SensorManager and implement SensorEventListener. 一种简单的方法是使用SensorManager并实现SensorEventListener。 The basic idea is you use the SensorManager to register the Orientation sensor and then respond to changes in the orientation of the device in the onSensorChanged delegate method implemented with SensorEventListener. 基本思想是使用SensorManager注册方向传感器,然后在通过SensorEventListener实现的onSensorChanged委托方法中响应设备方向的更改。 Make sure you unregister the listener onPause() or else it will kill your battery. 确保在onPause()上取消注册侦听器,否则将耗尽电池电量。

As a high level example: 作为高级示例:

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
}

note: The orientation sensor has been deprecated, although I still think it works best. 注意:虽然我仍然认为方向传感器效果最好,但已经弃用了方向传感器。 The updated approach is below if you'd like to try that. 如果您想尝试更新的方法,请参见下面的方法。

From the android documentation: https://developer.android.com/guide/topics/sensors/sensors_position.html#sensors-pos-orient 来自android文档: 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.
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM