Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Joker123   Members   -  Reputation: 115

Like
0Likes
Like

Posted 24 December 2013 - 09:24 AM

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;

    void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
    {
        UpdateUI(e.SensorReading);
    }

    private void UpdateUI(MotionReading e)
    {               
        float accelerometerX = e.DeviceAcceleration.X;
        float accelerometerY = e.DeviceAcceleration.Y;
        speed.X += accelReading.X;
        speed.Y += -accelReading.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.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
            motion.Start();
        }

        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Message = Content.Load<SpriteFont>("Arial");
        Ball = Content.Load<Texture2D>("ballbig");
    }

    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);
    }
}


Sponsor:

#2 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 24 December 2013 - 02:56 PM

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, 24 December 2013 - 03:19 PM.


#3 Joker123   Members   -  Reputation: 115

Like
0Likes
Like

Posted 25 December 2013 - 04:20 AM

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?



#4 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 26 December 2013 - 02:34 PM

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, 26 December 2013 - 02:37 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS