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
Pinch Zoom Touch Panel Tutorial, in
- Programming the accelerometer
2. Demo Program Behavior
Tilt the phone around to see:
- The basketball "falling" according to gravitational pull
The large orange arrows showing the current downward acceleration magnitudes
in the x and y directions.
The following is an example screen shot of what you may see.
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:
- Source code files: The implementation are contain in two files:
In our discussion, we will first look at AccelerometerDemo.cs followed by examining the implementation of the event
- AccelerometerDemo.cs: implements the XNA framework
functions and supports user interaction.
- AccelerometerDemo_Compute.cs: implements event service routines for
- External Resources: there are the usual background music and
sound effects, including two image objects: the basketball and the
background maze images. Other supporting images we will not be discussing
include the support for visualization of the accelerometer states (the large
orange arrows), and the display for coordinate markers: upper-left red
square representing the origin, the W marker representing (W, 0), and the H
marker representing the (0, H) positions. Please refer to the
Orientation Tutorial for
discussions on the coordinate markers.
4. Implementation Details:
4a. AccelerometerDemo.cs: Implements
XNA framework functions and supports user interaction
The Initialize() function:
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:
- mAccMeter: this object is our interface to the
accelerometer sensors. We will initialize this object, and through
it, activate the sensors, and receive data.
- 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 Update() Function:
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).
- Instantiate the accelerometer object,
- Register a event service routine (AccMeterChange()) to
service accelerometer reading changes, and
- Attempt to "start" the accelerometer sampling.
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
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.
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
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
5. Self Test and Exercises
- If gravitational pull is -1, why does the accelerometer range supports to
values of -2? Try jerking the phone of give the phone a sudden shake to see the
orange arrow size grow very large as a result. Remember, F=ma, mass of the phone
is constant, if you apply a large force to the phone, you will get a large
- Why can't I call mBall.SetAcceleration() directly from
AccMeterChange() service routine? Answer, AccMeterChange() is invoked
asynchronously from the accelerometer sampling thread. It is very possible that
the AccMeterChange() function is called at the very same time when
mBall.Update() is being called from our own Update() function. In
this case, we end up having two threads both trying to modify mBall's
acceleration values. We will end up with inconsistent results.
- If we replace the control of the mBall x-movement from using the X readings
from the accelerometer to using the z-readings, how will I direct the ball to
move leftward? Answer: leftward, is -x direction, to get negative values from
the z-accelerometer, I must flip my phone upside-down.
- Practice Project: give the ball a constant speed (e.g., 3) and use the
accelerometer reading to control the direction of the ball. In this case, the
ball should never speed up or slow down, just roll around reacting to how the
phone is tilted.
Computing and Software Systems
University of Washington, Bothell
Project home page:
The Game-Themed Introductory Programming Project.
||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