开发者

Android Orientation sensor for rotation the 3D cube

I'm trying to make 3-dof controller using Android phone, similar to Wiimote. Uses Accelerometer for recognizing the controller'开发者_如何学Cs 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.


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜