Game Development Reference
In-Depth Information
Shaking things up a bit
The accelerometer is a special sensor that can also determine movement, which can
be useful if you want to implement some form of shake detection. This is common
enough within standard apps that WinRT has a system that does the shake detec-
tion for you. The shake detection system is only accessible through the event sys-
tem; however, you don't need to set any intervals, just hook into the Acceleromet-
er::Shaken event to know when the device has been shaken.
As usual you need to get the default accelerometer, and once you have that you
need to bind a TypedEventHandler to the Shaken event, specifying the Ac-
celerometer^ and AccelerometerShakenEventArgs as template types, as
shown:
accelerometer->Shaken::add(
ref new TypedEventHandler<
Accelerometer^, AccelerometerShakenEventArgs^>
(
this,
&MyClass::Shaken
));
All you get from that event call is the accelerometer and the event arguments, which
just contain a timestamp. The focus here is on simplicity, so you can tell Windows
that you want to know when the user shakes the device, and you don't need raw val-
ues or details.
Remember that these are not mutually exclusive, and you can use any of the three
methods in any combination. As you will see, the focus of the API is to make it easy
to get the information you want, and in doing so you have the freedom to do what
you need to do.
Spinning with gyros
That's how you work with an accelerometer; let's look at how to work with a gyro-
scope. Gyroscopes help to measure the orientation of the device by providing the
angular velocity (rotation speed) of the device. If you track the angular velocity, you
Search WWH ::




Custom Search