You want to monitor an object on the screen using an acceleration sensor.
protected override void Initialize() { ... Accelerometer acc = new Accelerometer(); acc.ReadingChanged += AccReadingChanged; acc.Start(); ... }
This is a method that calculates the position of an object.
void AccReadingChanged(object sender, AccelerometerReadingEventArgs e) { // Y axes is same in both cases this.circlePosition.Y = (float)eZ * GraphicsDevice.Viewport.Height + GraphicsDevice.Viewport.Height / 2.0f; // X axes needs to be negative when oriented Landscape - Left if (Window.CurrentOrientation == DisplayOrientation.LandscapeLeft) this.circlePosition.X = -(float)eY * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; else this.circlePosition.X = (float)eY * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; }
I use the Z axis of the sensor as my Y in the game and the Y axis of the sensor as my X in the game. Calibration will be performed by subtracting the Z axis of the sensor from the center. Thus, our sensor axes directly correspond to the position (in percent) on the screen.
For this, we donβt need the X axis of the sensor at all ...
This is just a quick implementation. You will find the center for the sensor, since this Viewport.Width / 2f not the central, total and average of the three dimensions, calibration along the X-axis, so you can play / use the application in a flat or some position, etc.
This code has been tested on a Windows Phone device! (and working)
source share