Android Orientation sensor for rotation the 3D cube

久未见 提交于 2019-12-12 09:05:10

问题


I'm trying to make 3-dof controller using Android phone, similar to Wiimote. Uses Accelerometer for recognizing the controller's orientation (used getOrientation() method for calculation)

I'm testing the orientation values by using those values to rotate the cube drawn by opengl in PC. The problem is, it doesn't seem working. If the phone is rotated over the specific rotation, the cube is rotated to some weird direction.

Without knowledge of computer graphics, I found the reference saying that in Euler rotation, the final figure of 3D object depends on the order of rotation on each axes. Is it related to the problem?? If so, what is the correct order? Current order is "yaw->pitch->roll"

I don't think it's because of the so-called calibration issue, as the value changes are significant.


回答1:


The Orientation sensor is deprecated. The best way to acquire reliable sensor values is using the rotation vector sensor. It is a software-based sensor that derives the data from the accelerometer and magnetometer hardware-based sensors.

The rotation vector represents the orientation of the device as a combination of an angle and an axis, in which the device has rotated through an angle θ around an axis (x, y, or z). The following code shows you how to get an instance of the default rotation vector sensor. See the info about this sensor in the Android Dev Site.

This is an example of how to use the rotation vector to get reliable values:

public void onSensorChanged(SensorEvent event) {
       if(sensor.getType()==Sensor.TYPE_ROTATION_VECTOR){
           float[] rotationMatrix = new float[16];
           SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
           SensorManager.getOrientation(rotationMatrix, mOrientValues);
           for(int i=0; i<3; i++)
              mOrientValues[i]=(float)
                  Math.toDegrees(mOrientValues[i])+180.0f;//orientation in degrees
}



回答2:


I had similar problem with getOrientation() returning weird results when phone approaches vertical position and no calibration helps.

The easy solution is to use this sensor:

manager = (SensorManager) context.getApplicationContext()
                                 .getSystemService(Context.SENSOR_SERVICE);
Sensor orientationSensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);`

Apparently this sensor is deprecated in newer platforms but it works just fine anyway.



来源:https://stackoverflow.com/questions/5499862/android-orientation-sensor-for-rotation-the-3d-cube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!