MOBILE

Using Windows Phone 7 Technologies : Understanding Orientation and Movement

1/24/2011 4:33:56 PM
When you hold a Windows Phone in your hand with its display facing you, think of it as occupying the origin of a three-dimensional graph with its z axis pointing toward you (a positive direction), its y axis pointing down (a negative direction), and its x axis pointing toward the right (a positive direction). Figure 1 shows how these three axes are positioned relative to the device as you hold it facing toward you in your hand.
Figure 1. Accelerometer axis direction when you hold it in your hand facing toward you

To illustrate the accelerometer reading of the (x, y, z) = (0, −1, 0) means putting the phone on the table, standing up with the phone buttons facing downwards, with the front of the phone facing toward you, as shown in Figure 2.

If you were to rotate the phone in Figure 6-2 to the right 90 degrees, so that the Windows Phone control buttons are to the right, as shown in Figure 3, and the expected accelerometer readings will be (x, y, z) = (-1, 0, 0).

If you took the phone in Figure 2 and rotated it to the right 180 degrees, (x, y, z) would be (0, 1, 0), as shown in Figure 4.

If you were to rotate the phone in Figure 4 to the right 90 degrees, as shown in Figure 5, (x, y, z) would be (1, 0, 0).

If you were to put the phone flat on the table with the phone facing up, as shown in Figure 6, (x, y, z) would be (0, 0, 1).

If you put the phone facing down on the table, as shown in Figure 7, (x, y, z) would be (0, 0, 1).

Figure 2. (x, y, z) = (0, −1, 0)

Figure 3. (x, y, z) = (−1, 0, 0)

Figure 4. (x, y, z) = (0, 1, 0)

Figure 5. (x, y, z) = (1, 0, 0)

Figure 6. (x, y, z) = (0, 0, −1)

Figure 7. (x, y, z) = (0, 0, 1)

1. Calculating Distance

The so-called Euclidean distance algorithm is a useful way to calculate a distance between two points in three-dimensional space. This is a very useful equation that allows you to detect sudden movements such as the shaking of the phone.

If ( Ox, Oy, Oz ) is a previous accelerometer value and ( Nx, Ny, Nz ) is a new one, you can calculate Euclidean distance as follows.



2. Calculating Pitch and Roll

With obtained accelerometer readings you will have a pretty good understanding of the current orientation of the phone as seen in foregoing section, but using the accelerometer data, you can obtain much more information to understand how the phone is tilted on the x, y, and z axes. This information can be very useful if you are planning to create airplane simulation games or racing games that use the accelerometer to control the direction of moving objects. Think of it as using a phone like joystick by detecting the tilt motion.

When you hold the phone vertically, with the screen facing you, both the pitch and roll angles are 0 degrees, as shown in Figure 8.

Figure 8. Pitch and roll angles of 0 degrees

Now if you tilt the phone slightly to the right, you will be able to calculate the pitch (ρ) and roll (φ) angles shown in Figure 6-9. Also there is another angle of interest, which would be theta, the angle respect to the z axis, which is not shown in Figure 9.

Figure 9. Pitch and roll angles

In order to calculate pitch (ρ), roll (φ), and theta angles you will need the following equations, where Ax, Ay, and Az are the accelerometer values for x, y, and z.



You'll use both Euclidean distance and pitch and roll calculations in the examples that follow.

Other  
  •  Programming the Mobile Web : HTML 5 (part 4) - Client Storage
  •  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
  •  Programming the Mobile Web : HTML 5 (part 2) - The canvas Element
  •  Programming the Mobile Web : HTML 5 (part 1)
  •  Windows Phone 7 : Submitting Your First Windows Phone Application to the Windows Phone Marketplace
  •  Windows Phone 7 : Packaging, Publishing, and Managing Applications
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 3)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 1)
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 2) - JavaScript Mobile UI Patterns
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 1) - JavaScript UI Libraries
  •  Windows Mobile Security - Kernel Architecture
  •  Windows Mobile Security - Introduction to the Platform
  •  iPhone Programming : Table-View-Based Applications - Building a Model
  •  Mobile Application Security : The Apple iPhone - Push Notifications, Copy/Paste, and Other IPC
  •  Mobile Application Security : The Apple iPhone - Networking
  •  Windows Phone 7 Development : Handling Device Exceptions
  •  Registering a Windows Phone Device for Debugging
  •  Programming the Mobile Web : WebKit CSS Extensions (part 5) - Transformations
  •  Programming the Mobile Web : WebKit CSS Extensions (part 4) - Animations
  •  
    Most View
    ASP.NET State Management Techniques : Understanding the Application/Session Distinction
    Kindle Fire HD - Most Advanced 7" Tablet (Part 1)
    Transact-SQL in SQL Server 2008 : Row Constructors
    Lenovo ThinkPad Edge S430 - Classic Business Laptop
    ASP.NET State Management Techniques : Maintaining Session Data
    G-360 And G-550 Power Supply Devices Review (Part 1)
    SQL Server 2008 : Index maintenance
    The Linux Build: Part For Penguins (Part 1)
    Three-resistance Xperia Go
    Home Security On A Budget (Part 2)
    Top 10
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 7)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 6)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 5) : Thermaltake Armor Revo
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 4) : Thermaltake Level 10 GTS
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 3) : Thermaltake Commander MS-III
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 2)
    Thermaltake Cases Are Suitable For Everyone’s Budget (Part 1) : Thermaltake Commander MS-I
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 3)
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 2)
    LG Optimus L9 - A Cheap Middle Class Android Phone (Part 1)