Using an Accelerometer to Build a Game Controller

The XNA team created their XNA Game Studio to simplify the creation of games for: Windows, the Xbox 360, the Zune, and the Windows Phone 7 Series. The Game Studio includes the XNA Framework--a set of managed libraries that are designed for game development. Version 3.1 allows you to create games for the PC, the Xbox 360, and the Zune while version 4.0 adds support for the Windows Phone 7 Series.

The XNA Game Studio is designed to work with Microsoft Visual Studio—and allows you to write games in C#.

If Visual Studio 2008 is installed on your PC, you can download XNA Game Studio 3.1 from the XNA creator’s club site. And, once you download the Game Studio, you’ll find a sample game under the …\Visual Studio 2008\Templates\ProjectTemplates\Visual C#\XNA Game Studio 3.0 folder. This sample game, RacingGameWindows, is a complete auto racing game that runs on a PC or the Xbox 360. The game allows the user to customize their car, to choose the level of difficulty for the track, and to race a pace car. The game includes rich sound and lighting features. And, the game also includes code that responds to the physics of the car and the track—creating realistic responses, for example, to over-acceleration or under-steering.

In its default state, the game accepts input from the keyboard, the mouse, or an Xbox 360 controller. For example, with the keyboard, the user can accelerate the car by pressing the up-arrow (↑) or slow the car down by pressing the down-arrow (↓); the user can steer left by pressing the left-arrow (←) and steer right by pressing the right-arrow (→).

With minor additions to three source files, and using the Sensor and Location platform, we were able to add support for another input device: a dual-axis accelerometer. The updates to the source files consisted of: 1 new line in the file basegame.cs, 13 new lines in the file carphysics.cs, and 46 new lines in the file input.cs. (Minimal changes when you consider the number of lines of new code.)

Once support for the Sensor platform was added, the user can control the car by moving an accelerometer in their hand. When they tilt the accelerometer along its positive Y-axis, the car accelerates, when they tilt the accelerometer along the negative Y-axis, the car slows down. When they tilt the accelerometer along the positive X-axis, the car steers to the right; when they tilt the accelerometer along the negative X-axis, the car steers to the left.

The following screenshot shows a user controlling their car using a circuit board that contains the Memsic dual-axis accelerometer:


The circuit board in the previous illustration was built using the Memsic dual-axis accelerometer and the Parallax BASIC Stamp 2 programmable microcontroller. Both of these components, the circuit board itself, and the USB-to-Serial cable (in the picture) can be ordered from the Parallax Corporation in Rocklin, California. To help you get started building the circuit, we’ve provided a whitepaper that you can download from here. (You can download sample firmware files from this same location).

So, you’re probably asking, how does the accelerometer data reach the racing game? Good question. The linkage between the sensor device and the Sensor API is a sample driver which you can download from here. This driver “listens” for input from the accelerometer and passes that input to the Sensor API. By invoking Sensor API methods and properties in the racing game, we are able to monitor changes in the sensor state and use this data to control the car.

We’ve provided a whitepaper that describes the necessary changes to the racing game. In addition, we’ve provided the three updated source files and a COM Interop that allows you to access the Sensor platform through .Net. You can download the documentation, the updated source files and the COM interop, using the Downloads tab on our Build Your Own Game Controller page on the MSDN Code Gallery.

-- Sensor & Location Platform Team

This posting is provided "AS IS" with no warranties and confers no rights.

Comments (1)

  1. sanajay patel says:


Skip to main content