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.
Reader Comments