Hi,
here is code from one of my games, it’s old but working - you can take a look. the only issue - on tablets it might mess axis, so you need to consider “native device orientation”
static Accelerometer _accelSensor;
static public void Start()
{
if (_accelSensor == null)
{
_accelSensor = new Accelerometer();
_accelSensor.CurrentValueChanged += _accelSensor_CurrentValueChanged;
_accelSensor.Start();
}
}
static void _accelSensor_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
//need to consider orientation here,if support only landscape might be like this
if (Settings.IsLandscape)
{
GravityDirection.Y = -(float)e.SensorReading.Acceleration.Y*gravityCoeff;
GravityDirection.X = -(float)e.SensorReading.Acceleration.X * gravityCoeff;
}
else
{
GravityDirection.Y = -(float)e.SensorReading.Acceleration.X * gravityCoeff;
GravityDirection.X = (float)e.SensorReading.Acceleration.Y * gravityCoeff;
}
}
}
I know this is an old topic now but I’m replying anyway because information on the accelerometer does seem to be a bit sparse, so maybe it’ll help someone else.
I just had a go at using it and it seemed fine. Here’s what I did:
Declare Accelerometer acc = new Accelerometer() at the start of the Game1 class automatically generated by Monogame.
Put acc.Start() in the Initialize() method.
Done.
Now I just call acc.CurrentValue.Acceleration when needed.