How can I use Windows Phone sensors to move an object?

Recommended Posts

I want to use the Windows Phone sensors to move a ball in a Windows Phone game. I tried to move the ball with the accelerometer, but the ball doesn't move correctly if I tilt my Windows Phone device(Lumia 920) in a direction.
For example, if I tilt the device to the left, the ball isn't moving to the left, instead the ball moves very strange. I don't know what I could change in my code so that the ball moves correctly.
What is wrong? Why is the ball not moving in the right direction when I tilt my Windows Phone device?

What should I change so that the ball moves correctly if I tilt the Windows Phone device?

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Motion motion;
Texture2D Ball;
Vector2 BallPos = new Vector2(400, 300);
Vector2 BallVelocity;

{
}

{
float accelerometerX = e.DeviceAcceleration.X;
float accelerometerY = e.DeviceAcceleration.Y;
BallPos.X += speed.X *0.05f;
BallPos.Y += speed.Y *0.05f;
}

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
graphics.IsFullScreen = true;
}

protected override void Initialize()
{
if (Motion.IsSupported)
{
motion = new Motion();
motion.Start();
}

base.Initialize();
}

{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void Update(GameTime gameTime)
{

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(Ball, BallPos, null, Color.White, 0, new Vector2(Ball.Width/2,Ball.Height/2), 1f, SpriteEffects.None,1);
spriteBatch.End();

base.Draw(gameTime);
}
}


Share on other sites

I haven't messed with the motion sensors on the windows phone, but you may need to take average out the inputs as opposed to directly translating the accelerometer input to values.  A person will not move a phone steadily.  The values will constantly move up and down which will cause your motion to be jagged and it will behave weird.

I would take a couple of readings (couple of nano seconds?) and average the readings to see where it's mostly moving towards.  I would then apply that number to the acceleration for my object and update the object within the update(gameTime) method.  As for how you would implement this, I would google how others have done it.  Maybe google "interpolating accelerometer" or something of that sort would help.

EDIT: Actually interpolation ISN'T what you would use here.  See if this helps you out.  Also, I think you may need to call motion.Start() in your initialization code

http://blogs.windows.com/windows_phone/b/wpdev/archive/2010/09/08/using-the-accelerometer-on-windows-phone-7.aspx

Edited by SonicD007

Share on other sites

I don't know how to use this formula in my code: On = On-1 + ?(In – On-1)

How can I use the formula or a similar formula in my code?

Share on other sites

I don't know how to use this formula in my code: On = On-1 + ?(In – On-1)

How can I use the formula or a similar formula in my code?

I skimmed through the article and it seems they're using extrapolation to smooth out the data.  Someone else can chime in if this is too overkill for what you're doing, but the formula seems to be the formula of a line i.e. y= m(x-x1) + y1

If you've taken calc 1 that formula should be familiar to you (or pre calc)

I'm not sure what On-1 stands for in this context, it seems like it's an initial reading.  Maybe it's the first reading that's gotten over the  50 inputs they get?

In is the raw input data which the sensor would give you.  I haven't researched if the SDK gives you the reading filtered or not or if it really is raw data.  You'll have to research that.  Hopefully something on the wikipedia page can help you a bit more.

http://en.wikipedia.org/wiki/Extrapolation

The article is using linear extrapolation

EDIT: From what I gathered, you would use the formula to sample 50 inputs within a given amount of time and average them out using that formula to have a smooth input. alpha (?) would be 0 to 1 which is basically how much smoothing should be done.  It's a constant.

Edited by SonicD007

Create an account

Register a new account

• Forum Statistics

• Total Topics
627736
• Total Posts
2978857

• 10
• 10
• 21
• 14
• 12