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
« SoundEffects in Windows Phone Silverlight | Main | OK Go Rube Goldberg Machine Wonder »
Monday
Mar222010

Windows Phone Accelerometer Support in XNA

Signs

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 References to make this work.

While writing this I tried to use keyboard input to simulate the accelerometer, and I found that there is no XNA keyboard support in the emulator, which is fair enough I suppose (although I’d like it really).

It does really weird things in the debugger though, I got all kinds of strange readings if I tried to find out what keys are pressed at a breakpoint when using the immediate mode window.

Of course, I’ve not been able to try this on  a real device (I wish) but if anyone out there does I’d love to know if it works or not.

Reader Comments (8)

Cheers for that Rob, i'll give it a try tomorrow
March 23, 2010 | Unregistered CommenterHarry Overs
Are you in Korea Rob?! I fly in on Friday (Seoul time).
March 24, 2010 | Unregistered CommenterAndrew
Photos look like Youngsan?
March 24, 2010 | Unregistered CommenterAndrew
Ah i see the photo was taken in 2007, but updated today. Never mind!
March 24, 2010 | Unregistered CommenterAndrew
I'm not in Korea. Great place though. I put up a picture every day and since I don't always get around to taking one I dip into the archive now and then.

The pictures were taken during the 2007 Imagine Cup World Finals in Seoul. Wonderful place, I'd love to go back there someday
March 24, 2010 | Registered CommenterRob
Hah am I actually the only reply to your awesome article?
May 27, 2010 | Unregistered CommenterKathie Leonard
Can't get this class to compile. Have added ref to Devices.Sensors as you specify, but AccelerometerSensor and AccelerometerReadingAsyncEventArgs can't be resolved.
December 23, 2010 | Unregistered CommenterOdhran
Can't get this class to compile. Have added ref to Devices.Sensors as you specify, but AccelerometerSensor and AccelerometerReadingAsyncEventArgs can't be resolved.

Reading more, seems MS have recently updated the Sensors namespace:

http://msdn.microsoft.com/en-us/library/microsoft.devices.sensors(v=VS.92).aspx
December 23, 2010 | Unregistered CommenterOdhran

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.