Compartir a través de


SensorManager.GetRotationMatrix Method

Definition

Computes the inclination matrix <b>I</b> as well as the rotation matrix <b>R</b> transforming a vector from the device coordinate system to the world's coordinate system which is defined as a direct orthonormal basis, where:

[Android.Runtime.Register("getRotationMatrix", "([F[F[F[F)Z", "")]
public static bool GetRotationMatrix (float[]? R, float[]? I, float[]? gravity, float[]? geomagnetic);
[<Android.Runtime.Register("getRotationMatrix", "([F[F[F[F)Z", "")>]
static member GetRotationMatrix : single[] * single[] * single[] * single[] -> bool

Parameters

R
Single[]

is an array of 9 floats holding the rotation matrix <b>R</b> when this function returns. R can be null. <p>

I
Single[]

is an array of 9 floats holding the rotation matrix <b>I</b> when this function returns. I can be null. <p>

gravity
Single[]

is an array of 3 floats containing the gravity vector expressed in the device's coordinate. You can simply use the android.hardware.SensorEvent#values values returned by a android.hardware.SensorEvent SensorEvent of a android.hardware.Sensor Sensor of type android.hardware.Sensor#TYPE_ACCELEROMETER TYPE_ACCELEROMETER. <p>

geomagnetic
Single[]

is an array of 3 floats containing the geomagnetic vector expressed in the device's coordinate. You can simply use the android.hardware.SensorEvent#values values returned by a android.hardware.SensorEvent SensorEvent of a android.hardware.Sensor Sensor of type android.hardware.Sensor#TYPE_MAGNETIC_FIELD TYPE_MAGNETIC_FIELD.

Returns

true on success, false on failure (for instance, if the device is in free fall). Free fall is defined as condition when the magnitude of the gravity is less than 1/10 of the nominal value. On failure the output matrices are not modified.

Attributes

Remarks

Computes the inclination matrix <b>I</b> as well as the rotation matrix <b>R</b> transforming a vector from the device coordinate system to the world's coordinate system which is defined as a direct orthonormal basis, where:

<ul> <li>X is defined as the vector product <b>Y.Z</b> (It is tangential to the ground at the device's current location and roughly points East).</li> <li>Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.</li> <li>Z points towards the sky and is perpendicular to the ground.</li> </ul>

<center><img src="../../../images/axis_globe.png" alt="World coordinate-system diagram." border="0" /></center>

<hr>

By definition:

[0 0 g] = <b>R</b> * <b>gravity</b> (g = magnitude of gravity)

[0 m 0] = <b>I</b> * <b>R</b> * <b>geomagnetic</b> (m = magnitude of geomagnetic field)

<b>R</b> is the identity matrix when the device is aligned with the world's coordinate system, that is, when the device's X axis points toward East, the Y axis points to the North Pole and the device is facing the sky.

<b>I</b> is a rotation matrix transforming the geomagnetic vector into the same coordinate space as gravity (the world's coordinate space). <b>I</b> is a simple rotation around the X axis. The inclination angle in radians can be computed with #getInclination. <hr>

Each matrix is returned either as a 3x3 or 4x4 row-major matrix depending on the length of the passed array:

<u>If the array length is 16:</u>

/  M[ 0]   M[ 1]   M[ 2]   M[ 3]  \
              |  M[ 4]   M[ 5]   M[ 6]   M[ 7]  |
              |  M[ 8]   M[ 9]   M[10]   M[11]  |
              \  M[12]   M[13]   M[14]   M[15]  /

This matrix is ready to be used by OpenGL ES's javax.microedition.khronos.opengles.GL10#glLoadMatrixf(float[], int) glLoadMatrixf(float[], int).

Note that because OpenGL matrices are column-major matrices you must transpose the matrix before using it. However, since the matrix is a rotation matrix, its transpose is also its inverse, conveniently, it is often the inverse of the rotation that is needed for rendering; it can therefore be used with OpenGL ES directly.

Also note that the returned matrices always have this form:

/  M[ 0]   M[ 1]   M[ 2]   0  \
              |  M[ 4]   M[ 5]   M[ 6]   0  |
              |  M[ 8]   M[ 9]   M[10]   0  |
              \      0       0       0   1  /

<u>If the array length is 9:</u>

/  M[ 0]   M[ 1]   M[ 2]  \
              |  M[ 3]   M[ 4]   M[ 5]  |
              \  M[ 6]   M[ 7]   M[ 8]  /

<hr>

The inverse of each matrix can be computed easily by taking its transpose.

The matrices returned by this function are meaningful only when the device is not free-falling and it is not close to the magnetic north. If the device is accelerating, or placed into a strong magnetic field, the returned matrices may be inaccurate.

Java documentation for android.hardware.SensorManager.getRotationMatrix(float[], float[], float[], float[]).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also

  • GetInclination(Single[])
  • <xref:Android.Hardware.SensorManager.GetOrientation(System.Single%5b%5d%2c+System.Single%5b%5d)>
  • <xref:Android.Hardware.SensorManager.RemapCoordinateSystem(System.Single%5b%5d%2c+Android.Hardware.Axis%2c+Android.Hardware.Axis%2c+Android.Hardware.Axis)>