Motion and device orientation for simple apps (Windows Runtime apps)
Most simple apps rely on a single sensor, the accelerometer, as an input device. Such apps typically use only one or two axes for input, but may also use the Shaken event as another input source. Simple apps may also incorporate an inclinometer.
Accelerometer axis orientation
If you are writing a simple game, you need to understand and take into account the axis orientation for various PC form factors and the tablet in particular. The following illustration shows the axis orientation for a tablet PC.
The next illustration shows the axis orientation for a laptop PC.
Inclinometer data: pitch, roll, and yaw
The terms roll, pitch, and yaw correspond to angular measurements about the three axes of an inclinometer.
The angle of rotation about the y-axis. Also called gamma.
The angle of rotation about the x-axis. Also called beta.
The angle of rotation about the z-axis. Also called alpha.
In the next illustration, the plane defined by the x-axis and y-axis is parallel to the earth’s surface.
To help explain the concepts of pitch, roll, and yaw, the next illustration shows each measurement as it applies to an aircraft flying straight and level with respect to the horizon.
In the case of a complex game, like an auto-racing game, the app could use the yaw value returned by the inclinometer to control the car’s steering. Note that this sort of app would differentiate between smaller devices, like a phone, and larger devices, like a tablet, to adjust the steering response. If you’ve written apps using DirectX or XNA, you’re familiar with a different axis orientation for roll and yaw. Note that for Windows 8, the y-axis maps to roll and the z-axis maps to yaw.
Simple app example
One example of a simple app is a labyrinth app that uses the x-axis and y-axis of the accelerometer to control one or more marbles rolling through a labyrinth or maze. (This app could also use the Shaken event to reset or restart the game.) The following illustration shows an example. Inputs along the y-axis would cause the marbles to move in either direction across the board; and, inputs along the x-axis would cause the marbles to move longitudinally along the board.
The app obtains acceleration values repeatedly on a particular time interval. Based on the time elapsed between successive acceleration events, the app calculates the associated change in velocity and applies it to the object in motion. The following pseudocode contains an example calculation.
Elapsed time = 20ms Acceleration X: .—0.265 G Acceleration Y: .524 G Delta Vx = 9.8 M/S^2 *-0.265 * .02 S = -0.05194 Delta Vy = (9.8 M/S^2 *0.524 * .02 S) * -1 = -0.102704
The app applies the velocity delta values in the previous example to the
Y components for each marble within its event handler. Note that the
Y delta value is multiplied by negative one (-1) to align the direction of travel with the screen coordinates.