public override void Update() { base.Update(); // Enable/disable supported sensors and update enabled sensors if (accelerometerSensor != null) { bool enable = accelerometerSensor.IsEnabled; if (enable != motionManager.AccelerometerActive) { if (accelerometerSensor.IsEnabled) { motionManager.StartAccelerometerUpdates(); } else { motionManager.StopAccelerometerUpdates(); } } if (enable) { var accelerometerData = motionManager.AccelerometerData; accelerometerSensor.Acceleration = accelerometerData != null?CmAccelerationToVector3(accelerometerData.Acceleration) : Vector3.Zero; } } if (compassSensor != null) { bool enable = compassSensor.IsEnabled; if (enable != locationManagerActivated) { if (compassSensor.IsEnabled) { locationManager.StartUpdatingHeading(); } else { locationManager.StopUpdatingHeading(); } locationManagerActivated = compassSensor.IsEnabled; } if (enable) { compassSensor.Heading = GetNorthInRadian(locationManager); } } if (gyroscopeSensor != null) { bool enable = gyroscopeSensor.IsEnabled; if (enable != motionManager.GyroActive) { if (gyroscopeSensor.IsEnabled) { motionManager.StartGyroUpdates(); } else { motionManager.StopGyroUpdates(); } } if (enable) { var gyroData = motionManager.GyroData; gyroscopeSensor.RotationRate = gyroData != null?CmRotationRateToVector3(gyroData.RotationRate) : Vector3.Zero; } } if (userAccelerationSensor != null) { bool enable = userAccelerationSensor.IsEnabled || gravitySensor.IsEnabled || orientationSensor.IsEnabled; if (enable != motionManager.DeviceMotionActive) { if (enable) { motionManager.StartDeviceMotionUpdates(); } else { motionManager.StopDeviceMotionUpdates(); } } if (enable) { var motion = motionManager.DeviceMotion; // Update orientation sensor if (motion != null && motion.Attitude != null) { var q = motionManager.DeviceMotion.Attitude.Quaternion; var quaternion = new Quaternion((float)q.x, (float)q.z, -(float)q.y, (float)q.w); if (compassSensor != null) { // re-adjust the orientation to align with the north (common behavior on other platforms) TODO current implementation only takes in account the first value. if (firstNorthValue <= 0) { firstNorthValue = GetNorthInRadian(locationManager); } quaternion = Quaternion.RotationY(-firstNorthValue) * quaternion; } orientationSensor.FromQuaternion(quaternion); } else { orientationSensor.Reset(); } // Update gravity sensor gravitySensor.Vector = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero; // Update user acceleration userAccelerationSensor.Acceleration = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero; } } }
public override void Update() { base.Update(); // Enable/disable supported sensors and update enabled sensors if (accelerometer != null) { bool enable = accelerometer.IsEnabled || (userAcceleration?.IsEnabled ?? false) || (gravity?.IsEnabled ?? false); bool isEnabled = windowsAccelerometer.ReportInterval != 0; if (enable != isEnabled) { windowsAccelerometer.ReportInterval = enable ? Math.Max(DesiredSensorUpdateIntervalMs, windowsAccelerometer.MinimumReportInterval) : 0; } if (enable) { accelerometer.Acceleration = GetAcceleration(windowsAccelerometer); } } if (compass != null) { bool enable = compass.IsEnabled; bool isEnabled = windowsCompass.ReportInterval != 0; if (enable != isEnabled) { windowsCompass.ReportInterval = enable ? Math.Max(DesiredSensorUpdateIntervalMs, windowsCompass.MinimumReportInterval) : 0; } if (enable) { compass.Heading = GetNorth(windowsCompass); } } if (gyroscope != null) { bool enable = gyroscope.IsEnabled; bool isEnabled = windowsGyroscope.ReportInterval != 0; if (enable != isEnabled) { windowsGyroscope.ReportInterval = enable ? Math.Max(DesiredSensorUpdateIntervalMs, windowsGyroscope.MinimumReportInterval) : 0; } if (enable) { var reading = windowsGyroscope.GetCurrentReading(); gyroscope.RotationRate = reading != null ? new Vector3((float)reading.AngularVelocityX, (float)reading.AngularVelocityZ, -(float)reading.AngularVelocityY) : Vector3.Zero; } } if (orientation != null) { bool enable = orientation.IsEnabled || (userAcceleration?.IsEnabled ?? false) || (gravity?.IsEnabled ?? false); bool isEnabled = windowsOrientation.ReportInterval != 0; if (enable != isEnabled) { windowsOrientation.ReportInterval = enable ? Math.Max(DesiredSensorUpdateIntervalMs, windowsOrientation.MinimumReportInterval) : 0; } if (enable) { var quaternion = GetOrientation(windowsOrientation); orientation.FromQuaternion(quaternion); if (userAcceleration.IsEnabled || gravity.IsEnabled) { // calculate the gravity direction var acceleration = GetAcceleration(windowsAccelerometer); var gravityDirection = Vector3.Transform(-Vector3.UnitY, Quaternion.Invert(quaternion)); var gravity = InputManager.G * gravityDirection; this.gravity.Vector = gravity; userAcceleration.Acceleration = acceleration - gravity; } } } }
public override void Update() { base.Update(); var quaternionArray = new float[4]; var rotationMatrixArray = new float[9]; var yawPitchRollArray = new float[3]; // Update sensors // Enable/disable supported sensors and update enabled sensors UpdateSensorPair(accelerometerListener, accelerometerSensor, (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector()); UpdateSensorPair(gyroscopeListener, gyroscopeSensor, (sensor, listener) => sensor.RotationRate = -listener.GetCurrentValuesAsVector()); UpdateSensorPair(linearAccelerationListener, userAccelerationSensor, (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector()); UpdateSensorPair(gravityListener, gravitySensor, (sensor, listener) => sensor.Vector = listener.GetCurrentValuesAsVector()); // Enabled/Disable/Update Orientation if (orientationListener != null) { bool enable = orientationSensor.IsEnabled || compassSensor.IsEnabled; // Orientation is used for compass as well if (enable != orientationListener.Enabled) { if (enable) { orientationListener.Enable(); } else { orientationListener.Disable(); } } if (enable) { // Update orientation base.Update(); var rotationVector = orientationListener.GetValues()?.ToArray() ?? new [] { 0.0f, 0.0f, 0.0f }; if (rotationVector.Length < 3) { return; } SensorManager.GetQuaternionFromVector(quaternionArray, rotationVector); SensorManager.GetRotationMatrixFromVector(rotationMatrixArray, rotationVector); SensorManager.GetOrientation(rotationMatrixArray, yawPitchRollArray); var quaternion = Quaternion.Identity; quaternion.W = +quaternionArray[0]; quaternion.X = +quaternionArray[1]; quaternion.Y = +quaternionArray[3]; quaternion.Z = -quaternionArray[2]; quaternion = Quaternion.RotationY(MathUtil.Pi) * quaternion; // align the orientation with north. orientationSensor.FromQuaternion(quaternion); // Update compass compassSensor.Heading = yawPitchRollArray[0] + MathUtil.Pi; } } }