Windows Phone 7 Sensors and XNA
Accelerometer: XNA AccelerometerDemo
Back to main page

Release 1.0 (XNA V4.0)

1. Topics Covered

This program demonstrates XNA support for the accelerometer on WP7. You may wish to download the source code, open the project and refer to the source code while follow the discussion. Topics covered in this tutorial includes: Pre-requisite: the following discussion refers to the through the Pinch Zoom Touch Panel Tutorial, in particular,


2. Demo Program Behavior

Tilt the phone around to see:

The following is an example screen shot of what you may see.

Portrait Image

The following discuss the implementation details.

3. Solution Structure and External Environment

The source code structure is similar to the structure of touch panel pinch zoom tutorial where we refer to the SimpleLibrary for image and font output. In addition:

4. Implementation Details:

4a. AccelerometerDemo.cs: Implements XNA framework functions and supports user interaction

An Image Image
The above listing shows a source code structure similar to to the structure from the Pinch Zoom Touch Panel Tutorial. The main difference is that in this case, at Label E, we have defined local variables to support the programming with the accelerometer:
  1. mAccMeter: this object is our interface to the accelerometer sensors. We will initialize this object, and through it, activate the sensors, and receive data.
  2. MShowAcceleration: this object is capable of showing the magnitude and size of vectors as arrows in the middle of the screen. This class is meant to help us visualize the accelerometer state and is completely independent from programming with the accelerometer. We will not go into the details of this class.
The Initialize() function:
An Image Image
The above listing shows that during initialization we must: We can consider the accelerometer being sampled by a separate thread. When interesting readings occurs, this sampling thread will send us an event asynchronously by calling the AccMeterChange() function (the details of this function is discussed later in this tutorial here).
The Update() Function:
An Image Image
The above listing shows that, since accelerometer readings occurs asynchronously, during the regular synchronous update there is actually nothing much to do. Here at Label U1, we simple invoke the mBall's update function. If mBall has non zero velocity or acceleration, it's position will be updated accordingly.

4b. AccelerometerDemo_Compute.cs: This file implements the services for the asynchronous events from the accelerometer sampling thread.

An Image Image
The above listing shows, as in all cases of XNA asynchronous event services, two functions are defined to service the asynchronous events from the sampling thread.
An Image Image
This function shows, accelerometer sampled values are returned as three floating point values (X, Y, Z), each in the range between -2 to 2. Normal gravitation pull is -1. In this case, we simply use the scaled (X,Y) values as the acceleration of the mBall to set the ball in motion.
An Image Image
This is the actual event service routine for the accelerometer reading change events. Notice that since this routine is invoked asynchronously by an external thread, it does not make any changes to any of the instance variables. Instead, this routine creates a lamba (function call) and request a thread-safe invocation of the function.

5. Self Test and Exercises


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Project home page: The Game-Themed Introductory Programming Project.

Microsoft Logo This work is supported in part by Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531, and Microsoft Higher Education.