I have been trying to learn XNA, and while I don't have access to books I've been relying on MSDN and tutorials to guide me along the way and I can now draw sprites and change their position and use Intersect and I am now trying to get a basic Accelerometer going for Windows Phone 7 and 8 phones.
I have some blocks and a ball. And I want to be able to move the ball (or roll it) by just tilting the phone up/down/left/right.
If you see below, the green dots represent areas where the ball can move. While the dark grey blocks are just boundaries where if the ball touches them it will bounce off of them (at a later time, I'm not concerned about this part yet).
My question is, how do I get the ball to respond correctly to tilt movements?
To try and get very basic tilt movements I have:
protected override void Initialize()
{
// TODO: Add your initialization logic here
Accelerometer accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();
base.Initialize();
}
void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
this.squarePosition.X = (float)e.SensorReading.Acceleration.Z * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f;
if (Window.CurrentOrientation == DisplayOrientation.LandscapeLeft)
{
this.squarePosition.X = +(float)e.SensorReading.Acceleration.X * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2;
}
else
{
this.squarePosition.X = (float)e.SensorReading.Acceleration.X * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2;
}
}
But it's absolutely shocking. It's very jittery, like, the object is constantly jumping around like it's having a seizure or something, and I am not even sure it this is the correct way to go about this.

Updatemethod, change the position depending on the latest value of the accelerometer. This way, if you inclinate the phone then stop moving, the ball will keep on moving in a smooth motion. Or you could use the gyroscope instead: msdn.microsoft.com/en-us/library/windowsphone/develop/… - Kevin GossesquarePosition.Xa value read from the Z axis of the accelerometer, then overwriting it with a value read from the X axis. I think one of the two assignments was supposed to be forsquarePosition.Yinstead - Kevin Gosse