快速入门:检索四元数和旋转矩阵 (HTML)

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

可以使用四元数传感器和用 JavaScript 编写的应用检索四元数和旋转矩阵。开发者通常使用此数据来控制复杂的游戏。

四元数可以最简单地理解为一个点 [x,y,z] 围绕单个任意轴的旋转。它不同于旋转矩阵,后者表示围绕三条轴的旋转。四元数后的数学非常奇特,因为它涉及到复数的几何属性以及虚数的数学属性,但是使用它们却非常简单,而且 DirectX 等框架也支持它们。

目标: 完成此快速入门后,你将了解如何使用方向传感器检索四元数和旋转矩阵。

先决条件

你应当熟悉 HTML、JavaScript 和事件。

你使用的设备或仿真器必须支持方向传感器。

完成所需时间: 15 分钟.

说明

1. 打开 Microsoft Visual Studio

打开 Visual Studio 的一个实例。

2. 创建新项目

创建新项目,从“JavaScript/应用商店应用”项目类型中选择“空白应用程序”****。

3. 替换 JavaScript 代码

打开你项目的 default.js 文件,并使用下列内容替换现有的代码。


// For an introduction to the Blank template, see the following documentation:
// https://go.microsoft.com/fwlink/p/?linkid=232509
(function () {
    "use strict";
    var orientationSensor;

    function id(elementId) {
        return document.getElementById(elementId);
    }

    function onDataChanged(e) {
        var reading = e.reading;
        id('txtQuaternion').innerHTML = "W: " + reading.quaternion.w.toFixed(6)
                                              + "X: " + reading.quaternion.x.toFixed(6)
                                              + "Y: " + reading.quaternion.y.toFixed(6)
                                              + "Z: " + reading.quaternion.z.toFixed(6);

        id('txtRotationMatrix').innerHTML = "M11: " + reading.rotationMatrix.m11.toFixed(6)
                                                  + "M12: " + reading.rotationMatrix.m12.toFixed(6)
                                                  + "M13: " + reading.rotationMatrix.m13.toFixed(6)
                                                  + "M21: " + reading.rotationMatrix.m21.toFixed(6)
                                                  + "M22: " + reading.rotationMatrix.m22.toFixed(6)
                                                  + "M23: " + reading.rotationMatrix.m23.toFixed(6)
                                                  + "M31: " + reading.rotationMatrix.m31.toFixed(6)
                                                  + "M32: " + reading.rotationMatrix.m32.toFixed(6)
                                                  + "M33: " + reading.rotationMatrix.m33.toFixed(6);
    }

    var app = WinJS.Application;

    // This function responds to all app activations.
    app.onactivated = function (eventObject) {
        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
            // Retrieve the default orientation sensor
            orientationSensor = Windows.Devices.Sensors.OrientationSensor.getDefault();

            // Choose a report interval supported by the sensor
            var minimumReportInterval = orientationSensor.minimumReportInterval;
            var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
            orientationSensor.reportInterval = reportInterval;

            // Set the event handler
            orientationSensor.addEventListener("readingchanged", onDataChanged);

            WinJS.UI.processAll();
        }
    };

    app.start();
})();

4. 为应用添加 HTML

打开 Windows 和 Windows Phone 项目的 default.html 文件,并将以下 HTML 复制到该文件的 BODY 标记内。


<div class="item" id="scenario1Output">
   Quaternion: <a id="txtQuaternion">no data</a>
   <br />
   Rotation Matrix: <a id="txtRotationMatrix">no data</a>
</div>

5. 生成、部署并运行应用

按 F5 或选择“调试”>“启动调试”****来构建、部署并运行应用。

应用运行后,你可以通过迁移设备或使用仿真器工具更改加速计的值。

6. 停止应用

  1. 按 ALT+Tab 切换到 Visual Studio。
  2. 按 Shift+F5 或选择“调试”>“停止调试”****来停止应用。

摘要和后续步骤

前面的示例演示了,只需要写入极少的代码即可将方向传感器集成到你的应用。

该应用在 onactivated 函数中建立了与默认方向传感器的连接。这通过下面一行实现。

orientationSensor = Windows.Devices.Sensors.OrientationSensor.getDefault();

onDataChanged 函数中捕获新数据。每次传感器驱动程序从传感器接收到新数据时,它都将使用此函数(或事件处理程序)将此值传递到你的应用中。应用在下行中注册此事件处理程序。

orientationSensor.addEventListener("readingchanged", onDataChanged);

通过对下面显示的 DOM 元素进行更新,将这些新值写到屏幕上。

<div class="item" id="scenario1Output">
   Quaternion: <a id="txtQuaternion">no data</a>
   <br />
   Rotation Matrix: <a id="txtRotationMatrix">no data</a>
</div>

该应用在 onactivated 函数中建立了报告间隔。此代码检索设备支持的最短间隔,并将它与所请求的间隔 16 毫秒(大约 60-Hz 刷新率)进行比较。如果支持的最短间隔大于所请求的间隔,则此代码会将报告间隔设置为所支持的最短间隔。否则,它会将报告间隔设置为所请求的间隔。

var minimumReportInterval = accelerometer.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
accelerometer.reportInterval = reportInterval;

如果你在编写三维应用,则后续步骤通常涉及到将四元数或旋转矩阵值与应用的图形输出相集成。

相关主题

OrientationSensor class

OrientationSensor 示例