I don't know how to use accelerometer in LibGDX; how could I use the the motion sensor? I'm working for a top down game with the motion sensor. I'm new to this framework. My sprite image is already working and I want to control it using the accelerometer.

All comments are much appreciated, thanks in advance :)

Here is my code:

public class IngameDayOne extends ApplicationAdapter implements Screen {

// Constant rows and columns of the sprite sheet
private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
// Objects used
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
private Texture cat ;
SpriteBatch spriteBatch;
Texture Background;
// A variable for tracking elapsed time for the animation
float stateTime;

public void create() {
    // Load the sprite sheet as a texture
    cat = new Texture(Gdx.files.internal("cat2.png"));

    float accelX = Gdx.input.getAccelerometerX();
    float accelY = Gdx.input.getAccelerometerY();

    if (accelX < -1){
        //go up
    if (accelY < -1 ){
        //go left
    if (accelY > +1){
        //go right

    // Use the split utility method to create a 2D array of TextureRegions. This is
    // possible because this sprite sheet contains frames of equal size and they are
    // all aligned.
    TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);

    // Place the regions into a 1D array in the correct order, starting from the top
    // left, going across first. The Animation constructor requires a 1D array.

    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
        for (int j = 0; j < FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
    // Initialize the Animation with the frame interval and array of frames
    walkAnimation = new Animation<TextureRegion>(0.090f, walkFrames);

    // Instantiate a SpriteBatch for drawing and reset the elapsed animation
    // time to 0
    spriteBatch = new SpriteBatch();
    stateTime = 0f;
    Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder

public void show() {

public void render(float delta) {


public void resize(int width, int height) {

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
    // Get current frame of animation for the current stateTime
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    spriteBatch.draw(currentFrame, 50, 50); // Draw current frame at (50, 50)

public void pause() {


public void resume() {


public void hide() {


public void dispose() { // SpriteBatches and Textures must always be disposed

1 Answer 1


The accelerometer (obviously) measures acceleration. This includes gravity too.

If you want to make the object move around when the user moves the phone around, then you must subtract the gravity out of it. This can be done by getting the device's orientation (try the rotation vector sensor), creating a vector that points downwards according to it, multiplying it with 9.81 (The gravity is on average 9.81 m/s²), and subtracting it from the acceleration vector. If you want to apply gravity to the object, then you can simply use the output of the sensor.

Next you need to add this vector to the player's velocity vector and then add the velocity vector the the played's position vector.

However, I advise you not to do this, because there are multiple problems with this:

  • You can't remove gravity from it easily. It will almost always have small error in it, which will make the player always move around, and you must introduce a system like in joysticks.
  • It is not very exact. If the velocity doesn't go back to 0, then the character will again move around, but now constantly in the same direction. If there's a very fast stop, then you'll encounter this issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .