Using the Accellerometer to roll a ball around an area

Topics: Developer Forum
Jun 21, 2014 at 5:17 PM
Hi there,

I have a simulation setup where I have a ball trapped within a circle. When the simulation starts the ball falls to the ground (bottom of the circle).

Ideally I want it to reflect the angle of the phone so that I can tip the phone to make the ball move in that direction, how do I achieve this?
        public void ApplyAccelerometerReading(AccelerometerReadingChangedEventArgs args)
        {
            if (Ball.BodyObject != null)
            {
                //Move ball here
            }
        }
The above is the method that I have ready to apply the reading to. I tried applying a force but the ball went all over the place, it wasn't very exact. Thanks in advance.
Jun 21, 2014 at 5:30 PM
Done,
        public void ApplyAccelerometerReading(AccelerometerReadingChangedEventArgs args)
        {
            if (Ball.BodyObject != null)
            {
                    float X = (float)args.Reading.AccelerationY < 0 ? (float)Math.Abs(args.Reading.AccelerationY) : 0 - (float)args.Reading.AccelerationY;
                    float Y = (float)args.Reading.AccelerationX < 0 ? (float)Math.Abs(args.Reading.AccelerationX) : 0 - (float)args.Reading.AccelerationX;
                    Ball.BodyObject.ApplyLinearImpulse(new Microsoft.Xna.Framework.Vector2(X, Y));
            }
        }