# The accelerometer

Platforms such as iOS and Bada have support for an accelerometer, which is responsible for measuring the acceleration of the device. As an example if a mobile device is tilted to one side the accelerometer can detect this and a game can react accordingly e.g moving the player around the screen based on the level of motion. Platforms such as Windows and Mac OS X do not have built in accelerometers, however, AGK provides a fallback mechanism whereby the accelerometer can be emulated through keyboard input. This means you can create an accelerometer based game that will run on all platforms without needing alteration.

This example demonstrates the usage of the accelerometer commands by placing a sprite on screen that has its movement controlled by any data coming from the accelerometer.

## Overview

The process for this example is as follows:

- set a virtual resolution
- create a sprite and place it near the center of the screen
- update our sprite's position based on accelerometer data

## Initial set up

The initial set up code deals with setting a virtual resolution of 320 x 480, followed with a call to create a sprite with an ID of 1 and position it near the center of the screen:

    SetVirtualResolution ( 320, 480 )

    CreateSprite ( 1, LoadImage ( "peach.png" ) )
    SetSpritePosition ( 1, 160, 200 )

    agk::SetVirtualResolution ( 320, 480 );

    agk::CreateSprite ( 1, agk::LoadImage ( "peach.png" ) );
    agk::SetSpritePosition ( 1, 160, 200 );

## Retrieving accelerometer data

Two commands are used to retrieve data from the accelerometer, they are GetDirectionX and GetDirectionY. Both commands take no parameters and return acceleration on the X and Y axis. When an accelerometer is present on the platform any values returned will come straight from the hardware, when no accelerometer is present (such as on Windows and Mac OS X) then the arrow keys are used.

When checking accelerometer data on the X axis, if the value is negative the device is tilting left or the left arrow key is being pressed. If the value that gets returned is positive then the device is tilting right or the right arrow key is being pressed.

When checking accelerometer data on the Y axis, if the value is negative the device is tilting upwards or the up arrow key is being pressed. If the value that gets returned is positive then the device is tilting downwards or the down arrow key is being pressed.

For our purposes, whenever accelerometer data is returned, these values will be applied to the sprite. This code must go within our main loop and is as follows:

    x# = GetDirectionX ( )
    y# = GetDirectionY ( )

    SetSpritePosition ( 1, GetSpriteX ( 1 ) + x#, GetSpriteY ( 1 ) + y# )

    float x = agk::GetDirectionX ( );
    float y = agk::GetDirectionY ( );

    agk::SetSpritePosition ( 1, agk::GetSpriteX ( 1 ) + x, agk::GetSpriteY ( 1 ) + y );

The code begins by saving the values returned from GetDirectionX and GetDirectionY into variables named x and y. These values are then printed on screen, so as to provide a visual aid in understanding the data being received. The final line sets the position of the sprite, by taking its original position and adding any values from the accelerometer, therefore as the device is tilted or the arrow keys pressed our sprite will move around the screen.

## Full code listing

Everything is now in place. An extra adjustment is made to ensure the sprite doesn't move off the screen, this is handled with the addition of a few if statements before calling Sync. Here's the final code for our program:

    SetVirtualResolution ( 320, 480 )

    CreateSprite ( LoadImage ( "background3.jpg" ) )

    CreateSprite ( 1, LoadImage ( "peach.png" ) )
    SetSpritePosition ( 1, 160, 200 )

    do
        x# = GetDirectionX ( )
        y# = GetDirectionY ( )

        Print ( "Tilt the device to move the sprite" )
        Print ( "Or use the arrow keys if a keyboard" )
        Print ( "is present" )
        Print ( "" )

        Print ( x# )
        Print ( y# )

        SetSpritePosition ( 1, GetSpriteX ( 1 ) + x#, GetSpriteY ( 1 ) + y# )

        if ( GetSpriteX ( 1 ) < 10 )
            SetSpriteX ( 1, 10 )
        endif

        if ( GetSpriteX ( 1 ) > 260 )
            SetSpriteX ( 1, 260 )
        endif

        if ( GetSpriteY ( 1 ) < 10 )
            SetSpriteY ( 1, 10 )
        endif

        if ( GetSpriteY ( 1 ) > 430 )
            SetSpriteY ( 1, 430 )
        endif

        sync ( )
    loop

    void app::Begin ( void )
    {
    agk::SetVirtualResolution ( 320, 480 );

    agk::CreateSprite ( agk::LoadImage ( "background3.jpg" ) );

    agk::CreateSprite ( 1, agk::LoadImage ( "peach.png" ) );
    agk::SetSpritePosition ( 1, 160, 200 );
    }

    void app::Loop ( void )
    {
        float x = agk::GetDirectionX ( );
        float y = agk::GetDirectionY ( );

        agk::Print ( "Tilt the device to move the sprite" );
        agk::Print ( "Or use the arrow keys if a keyboard" );
        agk::Print ( "is present" );
        agk::Print ( "" );

        agk::Print ( x );
        agk::Print ( y );

        agk::SetSpritePosition ( 1, agk::GetSpriteX ( 1 ) + x, agk::GetSpriteY ( 1 ) + y );

        if ( agk::GetSpriteX ( 1 ) < 10 )
            agk::SetSpriteX ( 1, 10.0f );

        if ( agk::GetSpriteX ( 1 ) > 260 )
            agk::SetSpriteX ( 1, 260.0f );

        if ( agk::GetSpriteY ( 1 ) < 10 )
            agk::SetSpriteY ( 1, 10.0f );

        if ( agk::GetSpriteY ( 1 ) > 430 )
            agk::SetSpriteY ( 1, 430.0f );
    }

## Conclusion

As you can see from this example, obtaining accelerometer data and acting on it doesn't require a great deal of code. What is also important is that this example will run on all platforms without any modification.