Partager via


Windows Phone 7 Emulator: Simulating the accelerometer

One of the challenges when using the
emulator is that yous simply don't have some hardware specific functionality,
such as the accelerometer. In order to overcome this, there are different
possible implementations:

 

1-Use the keyboard:

Probably the simplest alternative is to use
your computer keyboard. Once you have your app running on the emulator, you can
enable the keyboard input by pressing the "Pg Up" key. From that point on, you
can automate your code to recognize when it is running on the emulator, and
switch from the accelerometer to the keyboard input.

A great example is in the Windows
Phone 7 training kit
. Once you have it downloaded, look for a sample game
named "MarbleMaze". In the Accelerometer.cs class you will find something like
this:

if
(Microsoft.Devices.Environment.DeviceType ==
DeviceType.Device)

{

//
if we're on device, we'll just grab our latest reading from the accelerometer

}

else

{

//
if we're in the emulator, we'll generate a fake acceleration value using the
arrow keys

//
press the pause/break key to toggle keyboard input for the emulator

}

 

From that point on, the game deals with
keyboard input when on the emulator, as this:

 

KeyboardState keyboardState = Keyboard.GetState();

 

stateValue.Z
= -1;

 

if
(keyboardState.IsKeyDown(Keys.Left))

stateValue.X = -.1f;

if
(keyboardState.IsKeyDown(Keys.Right))

stateValue.X = .1f;

if (keyboardState.IsKeyDown(Keys.Up))

stateValue.Y = .1f;

if
(keyboardState.IsKeyDown(Keys.Down))

stateValue.Y = -.1f;

 

stateValue.Normalize();

 

Very simple and works
fine.

Now you may want a
little more precision than that, so next option would be:

2-Use the mouse:

Very similar to the
previous example, but now we can do something like:

 

MouseState newMouseState = Mouse.GetState();

stateValue.Z
=-1;

stateValue.X
= newMouseState.X - oldMouseState.X;

stateValue.Y
= oldMouseState.Y - newMouseState.Y;

 

oldMouseState.X
= newMouseState.X;

oldMouseState.Y
= newMouseState.Y;

stateValue.Normalize();

 

So this is a very
simplistic example (and by the way, would only work with the left button
pressed all the time) where we capture the delta of the current position and
previous position and use this to build our vector. We could extend this to
play with the Z value as well, either capturing some specific keyboard key and
swapping the Y for the Z or any other trick like that. Works pretty well for
me. There are other approaches for using the mouse here and I'm happy to point you to some examples if you need. Just ask me.

3-Use augmented
reality:

Now if you are really
ambitious about simulating the experience of holding the phone and moving it,
there are options such as this one. This cool
codeplex project
uses a webcam to simulate the accelerometer in a very
interesting way. You can read more about it in the Nikos
Kastellanos blog
.

4-Other options:

I've seen articles
showing ways of hacking certain videogame controllers and other devices for
that purpose but I don't think I should comment about those J People can be very creative sometimes...

Comments

  • Anonymous
    September 26, 2010
    Hi Mateus, congrats for the blog!