Udostępnij za pośrednictwem


How to align sensors with your app’s orientation

Brian Rockwell presents a quick overview showing how to align a device’s sensors with your app’s orientation:

The sensors supported by Windows Store apps offer a powerful way to spice up your app’s functionality. Sensors let your app know the relationship between the device and the physical world around it; the direction, orientation, and movement in those realms. These sensors can help make your game, augmented reality app, or utility app more useful and interactive by providing a unique form of input such as using the motion of the tablet to arrange the characters on the screen or to simulate the user being in a cockpit with the device as the steering wheel. 

Brian gives us a bit of code to calculate the gyrometer axes based on the app’s current display orientation:

 private void ReadingChanged(object sender, GyrometerReadingChangedEventArgs e)
 {
     GyrometerReading reading = e.Reading;  
     
     // Calculate the gyrometer axes based on
     // the current display orientation.
     DisplayInformation displayInfo = DisplayInformation.GetForCurrentView();
     switch (displayInfo.CurrentOrientation) 
     { 
         case DisplayOrientations.Landscape: 
             x_Axis = reading.AngularVelocityX;
             y_Axis = reading.AngularVelocityY;
             z_Axis = reading.AngularVelocityZ;
             break;
         case DisplayOrientations.Portrait: 
             x_Axis = reading.AngularVelocityY;
             y_Axis = -1 * reading.AngularVelocityX;
             z_Axis = reading.AngularVelocityZ;
             break; 
         case DisplayOrientations.LandscapeFlipped: 
             x_Axis = -1 * reading.AngularVelocityX;
             y_Axis = -1 * reading.AngularVelocityY;
             z_Axis = reading.AngularVelocityZ;
             break; 
         case DisplayOrientations.PortraitFlipped: 
             x_Axis = -1 * reading.AngularVelocityY;
             y_Axis = reading.AngularVelocityX;
             z_Axis = reading.AngularVelocityZ;
             break; 
      } 
     
     
     // Update the UI...
 }

More after the jump.

Aligning sensors with your app’s orientation

Technorati Tags: Windows,Windows store app,C#,Windows 8,Windows SDK,Windows programming