Retrieving quaternion data in complex apps (Windows Runtime apps)

An app can track changes in quaternion data by registering for an event notification from the OrientationSensor class (a combination of accelerometer, compass, and, optionally, the gyrometer).

The following code examples demonstrate how to retrieve the default OrientationSensor class for a device and then register a ReadingChanged event handler to receive notifications when the sensor issues a new reading.

_sensor = OrientationSensor.GetDefault();
sensor = Windows.Devices.Sensors.OrientationSensor.getDefault();
private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        // Establish the report interval
        _sensor.ReportInterval = _desiredReportInterval;

        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.ReadingChanged += new TypedEventHandler<OrientationSensor, OrientationSensorReadingChangedEventArgs>(ReadingChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;
    }
    else
    {
        rootPage.NotifyUser("No orientation sensor found", NotifyType.StatusMessage);
    }
}
function enableReadingChangedScenario() {
    if (sensor) {
        // Set the reportInterval to enable the sensor events
        sensor.reportInterval = reportInterval;

        document.addEventListener("msvisibilitychange", msVisibilityChangeHandler, false);
        sensor.addEventListener("readingchanged", onDataChanged);
        document.getElementById("scenario1Open").disabled = true;
        document.getElementById("scenario1Revoke").disabled = false;
    } else {
        WinJS.log && WinJS.log("No orientation sensor found", "sample", "error");
    }
}

The ReadingChanged event handler then receives the quaternion and rotation-matrix values as shown in the following example.

async private void ReadingChanged(object sender, OrientationSensorReadingChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        OrientationSensorReading reading = e.Reading;

        // Quaternion values
        SensorQuaternion quaternion = reading.Quaternion;   // get a reference to the object to avoid re-creating it for each access
        ScenarioOutput_X.Text = String.Format("{0,8:0.00000}", quaternion.X);
        ScenarioOutput_Y.Text = String.Format("{0,8:0.00000}", quaternion.Y);
        ScenarioOutput_Z.Text = String.Format("{0,8:0.00000}", quaternion.Z);
        ScenarioOutput_W.Text = String.Format("{0,8:0.00000}", quaternion.W);

        // Rotation Matrix values
        SensorRotationMatrix rotationMatrix = reading.RotationMatrix;
        ScenarioOutput_M11.Text = String.Format("{0,8:0.00000}", rotationMatrix.M11);
        ScenarioOutput_M12.Text = String.Format("{0,8:0.00000}", rotationMatrix.M12);
        ScenarioOutput_M13.Text = String.Format("{0,8:0.00000}", rotationMatrix.M13);
        ScenarioOutput_M21.Text = String.Format("{0,8:0.00000}", rotationMatrix.M21);
        ScenarioOutput_M22.Text = String.Format("{0,8:0.00000}", rotationMatrix.M22);
        ScenarioOutput_M23.Text = String.Format("{0,8:0.00000}", rotationMatrix.M23);
        ScenarioOutput_M31.Text = String.Format("{0,8:0.00000}", rotationMatrix.M31);
        ScenarioOutput_M32.Text = String.Format("{0,8:0.00000}", rotationMatrix.M32);
        ScenarioOutput_M33.Text = String.Format("{0,8:0.00000}", rotationMatrix.M33);
    });
}
function onDataChanged(e) {
    var reading = e.reading;

    // Quaternion values
    var quaternion = reading.quaternion;    // get a reference to the object to avoid re-creating it for each access
    document.getElementById("eventOutputQuaternion").innerHTML =
           "W: " + quaternion.w.toFixed(6)
        + " X: " + quaternion.x.toFixed(6)
        + " Y: " + quaternion.y.toFixed(6)
        + " Z: " + quaternion.z.toFixed(6);

    // Rotation Matrix values
    var rotationMatrix = reading.rotationMatrix;
    document.getElementById("eventOutputRotationMatrix").innerHTML =
           "M11: " + rotationMatrix.m11.toFixed(6)
        + " M12: " + rotationMatrix.m12.toFixed(6)
        + " M13: " + rotationMatrix.m13.toFixed(6)
        + " M21: " + rotationMatrix.m21.toFixed(6)
        + " M22: " + rotationMatrix.m22.toFixed(6)
        + " M23: " + rotationMatrix.m23.toFixed(6)
        + " M31: " + rotationMatrix.m31.toFixed(6)
        + " M32: " + rotationMatrix.m32.toFixed(6)
        + " M33: " + rotationMatrix.m33.toFixed(6);
}