Tag: ACCELEROMETER

Zune HD Accelerometer Basics

Posted by – October 2, 2009

I was just messing around with my Zune HD while taking a short break from my main project when I thought I could port my TinyEngine to it in no time.

Sure enough, it took about 12 seconds.

In my dummy project I have Mr Tiny just smiling away in the center of the screen.

mrtiny

I wanted to get him to move around by just using the accelerometer. This was really simple.

I just call Accelerometer.GetState(); which returns a AccelerometerState. The AccelerometerState contains a Acceleration of type Vector3 which contains the direction of the accelerometer. I then just pass the direction of tilt to my engines Sprite Move command, as a Vector2.

Something like this

Vector2 accel = new Vector2(state.Acceleration.X, -state.Acceleration.Y);
    AccelerometerState state = Accelerometer.GetState();
    Vector2 accel = new Vector2(state.Acceleration.X, -state.Acceleration.Y);
    this.mrTiny.Move(accel * 100 * elapsed);

Clamp it to the screen

    this.mrTiny.PositionX = MathHelper.Clamp(this.mrTiny.PositionX, this.ScreenBounds.Left, this.ScreenBounds.Right);
    this.mrTiny.PositionY = MathHelper.Clamp(this.mrTiny.PositionY, this.ScreenBounds.Top, this.ScreenBounds.Bottom);

 

And there we have it, you can tilt your Zune HD to move around Mr Tiny.

Short but sweet uh?

The code is linked just below. I’ll be looking to do more Zune HD samples in the near future, as well as adding gesture support to TinyEngine.

Once I’m finished with my secret project.

Source Code (Includes Tiny Engine Source)