Example code

The following sections detail the code samples that accompany this chapter.

The Gyroscope project

This project contains the complete source code for the Gyroscope extension developed throughout the course of this chapter. Compiled versions of the extension have also been included so you can build the other example projects for this chapter without having to first build the extension itself.

The GyroTest project

The GyroTest project is a simple example that makes use of the Gyroscope extension. It demonstrates how to include the Gyroscope extension into a project, how to check if the extension is available, and then how to call the extension function if it is available.

The sample will be displayed on screen whether or not gyroscope support is available. If it is, the raw gyroscope values will also be displayed on screen.

The Skiing project

Our final update to the Skiing project sees it make use of the Gyroscope extension developed in this chapter as another possible control method.

As with the other input methods in the game, a class called GyroscopeManager has been created, which wraps up the Gyroscope extension. This then keeps all use of the extension functions in a single source file, which makes it easier to update should we ever change the API of the extension in any way.

No matter how still the device is, even if left lying on a stable surface, the gyroscope values will always have a certain amount of jitter. The GyroscopeManager class deals with this by maintaining a filtered version of the gyroscope inputs that are used to control the skier in the game.

In every update of the main game loop, a new filtered value for each gyroscope axis is calculated by adding a percentage of the difference between the current filtered value and the new raw value for each axis to the current filtered value. This results in the smaller effects of jitter mostly being ignored without losing the larger intentional gyroscope inputs from the player.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.227.4