ジャイロメーターを使用してユーザーの動きの変化を検出する方法について説明します。
重要な API
[前提条件]
拡張アプリケーション マークアップ言語 (XAML)、Microsoft Visual C#、およびイベントについて理解している必要があります。
使用しているデバイスまたはエミュレーターは、ジャイロメーターをサポートしている必要があります。
シンプルなジャイロメーター アプリを作成する
ジャイロメーターは、加速度計をゲーム コントローラーとして補完します。 加速度計は直線運動を測定し、ジャイロメーターは角速度または回転運動を測定できます。
注
より完全な実装については、ジャイロメーターのサンプルを参照してください。
インストラクション
Visual C# プロジェクト テンプレートから Blank App (ユニバーサル Windows) を選択して、新しいプロジェクトを作成します。
プロジェクトのMainPage.xaml.cs ファイルを開き、既存のコードを次のコードに置き換えます。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core; // Required to access the core dispatcher object
using Windows.Devices.Sensors; // Required to access the sensor platform and the gyrometer
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private Gyrometer _gyrometer; // Our app' s gyrometer object
// This event handler writes the current gyrometer reading to
// the three textblocks on the app' s main page.
private async void ReadingChanged(object sender, GyrometerReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
GyrometerReading reading = e.Reading;
txtXAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX);
txtYAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY);
txtZAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ);
});
}
public MainPage()
{
this.InitializeComponent();
_gyrometer = Gyrometer.GetDefault(); // Get the default gyrometer sensor object
if (_gyrometer != null)
{
// Establish the report interval for all scenarios
uint minReportInterval = _gyrometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_gyrometer.ReportInterval = reportInterval;
// Assign an event handler for the gyrometer reading-changed event
_gyrometer.ReadingChanged += new TypedEventHandler<Gyrometer, GyrometerReadingChangedEventArgs>(ReadingChanged);
}
}
}
}
前のスニペットの名前空間の名前をプロジェクトに指定した名前に変更する必要があります。 たとえば、GyrometerCSという名前
- MainPage.xaml ファイルを開き、元の内容を次の XML に置き換えます。
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
<TextBlock HorizontalAlignment="Left" Height="23" Margin="8,8,0,0" TextWrapping="Wrap" Text="X-Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFDFDFD"/>
<TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="23" Margin="67,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="88" Foreground="#FFFDFAFA"/>
<TextBlock HorizontalAlignment="Left" Height="20" Margin="8,52,0,0" TextWrapping="Wrap" Text="Y Axis:" VerticalAlignment="Top" Width="46" Foreground="White"/>
<TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="24" Margin="54,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80" Foreground="#FFFBFBFB"/>
<TextBlock HorizontalAlignment="Left" Height="21" Margin="8,93,0,0" TextWrapping="Wrap" Text="Z Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFEFBFB"/>
<TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="21" Margin="54,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="63" Foreground="#FFF8F3F3"/>
</Grid>
</Page>
前のスニペットのクラス名の最初の部分をアプリの名前空間に置き換える必要があります。 たとえば、GyrometerCSという名前 xmlns:local="using:App1"
を xmlns:local="using:GyrometerCS"
に置き換える必要があります。
- F5 キー 押すか、[デバッグ]>[デバッグの開始] を選択して、アプリをビルド、デプロイ、および実行します。
アプリが実行されたら、デバイスを移動するかエミュレーター ツールを使用して、ジャイロメーターの値を変更できます。
- Visual Studio に戻り、Shift キーを押しながら F5 キーを押してアプリを停止するか、[デバッグ]
[デバッグの停止] 選択してアプリを停止します。
説明
前の例は、ジャイロメーター入力をアプリに統合するために記述する必要があるコードの量を示しています。
アプリは、MainPage メソッドの既定のジャイロメーターとの接続を確立します。
_gyrometer = Gyrometer.GetDefault(); // Get the default gyrometer sensor object
アプリは、MainPage メソッド内でレポート間隔を確立します。 このコードは、デバイスでサポートされている最小間隔を取得し、16 ミリ秒 (約 60 Hz のリフレッシュ レート) の要求された間隔と比較します。 サポートされる最小間隔が要求された間隔より大きい場合、コードは値を最小値に設定します。 それ以外の場合は、要求された間隔に値が設定されます。
uint minReportInterval = _gyrometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_gyrometer.ReportInterval = reportInterval;
新しいジャイロメーター データは、ReadingChanged メソッドでキャプチャされます。 センサー ドライバーは、センサーから新しいデータを受信するたびに、このイベント ハンドラーを使用して値をアプリに渡します。 アプリは、このイベント ハンドラーを次の行に登録します。
_gyrometer.ReadingChanged += new TypedEventHandler<Gyrometer,
GyrometerReadingChangedEventArgs>(ReadingChanged);
これらの新しい値は、プロジェクトの XAML で見つかった TextBlock に書き込まれます。
<TextBlock HorizontalAlignment="Left" Height="23" Margin="8,8,0,0" TextWrapping="Wrap" Text="X-Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFDFDFD"/>
<TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="23" Margin="67,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="88" Foreground="#FFFDFAFA"/>
<TextBlock HorizontalAlignment="Left" Height="20" Margin="8,52,0,0" TextWrapping="Wrap" Text="Y Axis:" VerticalAlignment="Top" Width="46" Foreground="White"/>
<TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="24" Margin="54,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80" Foreground="#FFFBFBFB"/>
<TextBlock HorizontalAlignment="Left" Height="21" Margin="8,93,0,0" TextWrapping="Wrap" Text="Z Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFEFBFB"/>
<TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="21" Margin="54,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="63" Foreground="#FFF8F3F3"/>