Game Development Reference
In-Depth Information
Orientation
There are multiple sensors that can be used independently or in combination to get
the orientation of a device. Many modern mobile PCs, from tablets to even laptops
have some or all of these sensors built-in. WinRT allows access to the individual
sensors, as well as a nice Orientation Sensor API that combines all of the provided
data and does the math required to give you the angles you want in a simpler form.
It's up to you what you use, depending on how much information you require.
Let's take a look at how to work with each sensor individually, and then look at the
Orientation Sensor API, which takes data from all of these and gives you the inform-
ation you need, saving you a lot of time.
Accelerometer
The accelerometer is a sensor that detects the direction and acceleration of the
device. This can be used to determine if the device is moving, but sometimes it is
used to determine the gravity direction to provide a reference to other sensors and
determine orientation.
For example, if the accelerometer is providing values indicating movement towards
the bottom of the device, close to the acceleration due to gravity, you know that the
device is upright.
There are two methods of accessing accelerometer data within your game. The first
is to listen for events providing updated accelerometer values. The second is to poll
for the values and, depending on the structure of your game, the latter may be the
more desirable option.
To work with the accelerometer, we need to use the Accelerometer class within
the Windows::Devices::Sensors namespace. To create an instance of this
class, just use the following code:
Accelerometer^ accelerometer =
Accelerometer::GetDefault();
Search WWH ::




Custom Search