×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

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

    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?
    Code:
    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);
        }
    }

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

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

    You may want to check the sensor readings themselves, prior to using them to control that ball.
    Some ideas can be borrowed from this article perhaps: http://developer.nokia.com/Community..._Windows_Phone, it also links to other articles, and to category page http://developer.nokia.com/Community..._Windows_Phone
    In short: according to its documentation, DeviceAcceleration is not suitable for what you are doing
    Quote Originally Posted by MSDN
    The raw data readings from the Accelerometer class include the force of gravity on the device in addition to forces caused by the user moving the device. The DeviceAcceleration property gives the acceleration of the device without the inclusion of gravity.
    it means that most of the time these readings are going to be around 0, as the gravitational component is not present. Any of the other MotionReading members may work better, especially Attitude (this may be what you need at the end) and Gravity (and this may be suitable for the way you are using the data) I think.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    2

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

    It works with attitude(pitch, roll). I tried this little tutorial: http://msdn.microsoft.com/en-us/libr.../jj155767.aspx
    But for my game, the sensors are a little bit too sensitive. For example, if my Luma 920 lies on a flat table, the ball is moving a little. What can I do so that the ball is not moving if my Lumia 920 lies on a flat table?

    My new code:

    Code:
    public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Motion motion;
            Texture2D Ball;
            Vector2 BallPos = new Vector2(400, 300);
            Vector3 accelReading = new Vector3();
            Vector3 speed = new Vector3();
            float DeltaVx, DeltaVy;
            float delta;
            float AccelerationFactor = 2.0f;
    
            void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
            {
                UpdateUI(e.SensorReading);
            }
    
            private void UpdateUI(MotionReading e)
            {
                accelReading.X = e.Attitude.Pitch;
                accelReading.Y = -e.Attitude.Roll;
    
                DeltaVx = (float)(9.8 * 9.8 * accelReading.X*delta*AccelerationFactor);
                DeltaVy = (float)(9.8 * 9.8 * accelReading.Y*delta*AccelerationFactor);
    
                speed.X = DeltaVx;
                speed.Y = DeltaVy;
    
                BallPos.X += speed.X;
                BallPos.Y += speed.Y;
            }
    
            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);
                Ball = Content.Load<Texture2D>("ballbig");
            }
    
            protected override void Update(GameTime gameTime)
            {
                delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
    
                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);
            }
        }

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

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

    If the readings are too sensitive, you can always filter them from code. It would be much-much harder in the other way (e.g. creating meaningful data from a constant 0).
    Calibrating the sensors (actually, the game) is also a good idea, all games do that I think. No magic is involved, just storing the sensor readings at the beginning of the level/round, and practically subtracting it from all readings afterwards. Then it does not matter if the user is playing upside down, leaning back, etc., since the "zero" position for the game is going to be the stored one.

Similar Threads

  1. Replies: 4
    Last Post: 2013-11-12, 09:58
  2. how to smoothly move object on accelerometer value change
    By sunder.tinwar in forum Windows Phone General
    Replies: 9
    Last Post: 2013-04-02, 06:06
  3. Allow access phone sensors
    By tarun.WRT in forum Symbian
    Replies: 3
    Last Post: 2010-03-18, 21:55
  4. how to move a m3g object
    By ronykucse in forum Mobile Java Games
    Replies: 1
    Last Post: 2010-02-05, 09:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •