Zune HD: XNA Accelerometer

October 24th, 2009 Categories: XNA, Zune HD

My Zune HD arrived last week and after getting over the initial disappointment of a disgracefully empty marketplace for it, I was really itching to try out some XNA and play around with the accelerometer.  First things first, I needed to download the XNA extensions that would provide the necessary framework to use the accelerometer in the Zune HD; these can be found right here.  As far as I’m aware, XNA for the older Zune won’t let you use the 3D API (I could be wrong??) and I don’t think this has been rectified by the XNA 3.1 Zune Extensions however hopefully an XNA 3.2 or 4.0 will come along in the near future and open that up.

UPDATE:

I verified that the 3D API isn’t available.  The likes of the Effect class aren’t available on the Zune.

In order to play with the accelerometer, I made a quick test project that allows the user to roll a ball about in a lovely blank black background. See this video:

Ok, so it’s not quite a ball….its a big white dot!  Who cares?

This was actually incredibly simple to achieve and was much easier than I had originally anticipated.  The XNA extensions provide a new class under Microsoft.Xna.Framework.Input called Accelerometer.  As with the other input devices, this class provides a GetState() method so it very much keeps in line with what XNA developers are already used to.  This method returns an object of type AccelerometerState which has a Vector3 property called Acceleration.  This property holds all the information you need for detecting tilt on the device.

Since the code is so simple, I don’t think I will walk through it step by step but rather I will just post my very quick code below.  Please ignore the rubbish boundary collisions – I didn’t feel like it was very important to make them perfect.  Rough and ready…just the way I like it!

Code Snippet
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. namespace AccelerometerTest
  6. {
  7.     public class Game1 : Microsoft.Xna.Framework.Game
  8.     {
  9.         private const float rollSpeed = 50f;
  10.         private GraphicsDeviceManager graphics;
  11.         private SpriteBatch spriteBatch;
  12.         private Rectangle boundary;
  13.         private Texture2D ballTexture;
  14.         private Vector2 ballPosition;
  15.         public Game1()
  16.         {
  17.             graphics = new GraphicsDeviceManager(this);
  18.             Content.RootDirectory = “Content”;
  19.             // Frame rate is 30 fps by default for Zune.
  20.             TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
  21.         }
  22.         protected override void Initialize()
  23.         {
  24.             boundary = GraphicsDevice.Viewport.TitleSafeArea;
  25.             base.Initialize();
  26.         }
  27.         protected override void LoadContent()
  28.         {
  29.             spriteBatch = new SpriteBatch(GraphicsDevice);
  30.             ballTexture = Content.Load<Texture2D>(“ball”);
  31.             ballPosition = new Vector2(boundary.Width / 2, boundary.Height / 2);
  32.         }
  33.         protected override void UnloadContent()
  34.         {
  35.             // TODO: Unload any non ContentManager content here
  36.         }
  37.         protected override void Update(GameTime gameTime)
  38.         {
  39.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  40.                 this.Exit();
  41.             Vector3 acceleration = Accelerometer.GetState().Acceleration;
  42.             Vector2 velocity = new Vector2(acceleration.X, -acceleration.Y) * rollSpeed;
  43.             Vector2 projectedPosition = ballPosition + velocity;
  44.             Rectangle projectedRectangle = new Rectangle((int)projectedPosition.X, (int)projectedPosition.Y, ballTexture.Width, ballTexture.Height);
  45.             if (projectedRectangle.Left < boundary.Left || projectedRectangle.Right > boundary.Right)
  46.                 velocity.X = 0;
  47.             if (projectedRectangle.Top < boundary.Top || projectedRectangle.Bottom > boundary.Bottom)
  48.                 velocity.Y = 0;
  49.             ballPosition += velocity;
  50.             base.Update(gameTime);
  51.         }
  52.         protected override void Draw(GameTime gameTime)
  53.         {
  54.             GraphicsDevice.Clear(Color.Black);
  55.             spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
  56.             spriteBatch.Draw(ballTexture, ballPosition, Color.White);
  57.             spriteBatch.End();
  58.             base.Draw(gameTime);
  59.         }
  60.     }
  61. }
Tags: , , ,

One Response to “Zune HD: XNA Accelerometer”

  1. October 24th, 2009 at 21:01
    1

    Don’t like the way that add in for Live Writer cut out all my blank lines!

Leave a Comment