Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« How do I play a sound effect in Silverlight? | Main | How Does the Emulator Actually Work? »
Thursday
Apr012010

How do I use the Accelerometer in the Windows Phone Emulator?

There is no native XNA support for the accelerometer in Windows Phone. It seems to have been moved into a different sensors class. I hope it doesn’t stay there. The accelerometer support in the Zune HD is fantastically easy to use and well in keeping with the other inputs.

I’ve written a tiny wrapper class that implements the accelerometer using the new sensor interface, but I really think this should really be put back into XNA, as event driven stuff really doesn’t sit well with the XNA philosophy of polling:

public class AccelerometerState
{
    public Vector3 Acceleration;

    public AccelerometerState()
    {
    }
}

public class Accelerometer
{
    static Accelerometer()
    {
        AccelerometerSensor.Default.ReadingChanged +=
            new EventHandler<AccelerometerReadingAsyncEventArgs>
                     Default_ReadingChanged);
    }

    static AccelerometerState state = new AccelerometerState();

    static void Default_ReadingChanged(object sender,
                                      AccelerometerReadingAsyncEventArgs e)
    {
        state.Acceleration.X = (float)e.Value.Value.X;
        state.Acceleration.Y = (float)e.Value.Value.Y;
        state.Acceleration.Z = (float)e.Value.Value.Z;
    }

    public static AccelerometerState GetState()
    {
        return state;
    }
}

You need to add the Microsoft.Devices.Sensors library to your project references to make this work.

I have also created an accelerometer simulator which uses an XNA gamepad to generate values which are hosted on a local web site and then consumed by a webclient in an XNA game on the phone. This works reasonably well. You can find the code for the XNA host and a game which consumes this here. Note that for this to work you must run the XNA program in Administraotr mode. You will also have to hard wire the IP address of the PC running the Windows Phone emulator into the XNA game running on the phone, so that it can get find the server.

References (2)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Rob Miles - Windows Phone FAQ - How do I use the Accelerometer in the Windows Phone Emulator?
  • Response
    Response: www.igscn.com
    Rob Miles - Windows Phone FAQ - How do I use the Accelerometer in the Windows Phone Emulator?

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.