AndEngine Tutorial 04 – Player Movement 02

Posted by Vexillum on August 08, 2012


In the 4th AndEngine tutorial we will learn how to use the build in accelerometer to be able to tell our player whether to move left or right.

Most Android devices have an accelerometer build in. This makes it a good user input method.

Let’s get started

There are several sensors supported by the SensorManager, the only one we need right now is TYPE_ACCELEROMETER.

    • A constant describing an accelerometer sensor type.
    • A constant describing all sensor types.
    • A constant describing an ambient temperature sensor type
    • A constant describing a gravity sensor type.
    • A constant describing a gyroscope sensor type
    • A constant describing a light sensor type.
    • A constant describing a linear acceleration sensor type.
    • A constant describing a magnetic field sensor type.
    • This constant is deprecated. Use SensorManager.getOrientation() instead.
    • A constant describing a pressure sensor type.
    • A constant describing an proximity sensor type.
    • A constant describing a relative humidity sensor type.
    • A constant describing a rotation vector sensor type.
    • This constant is deprecated. Use Sensor.TYPE_AMBIENT_TEMPERATURE instead.

Source :

package com.PerleDevelopment.AndEngine.tutorial.helper;

import android.content.Context;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerHelper extends Activity implements SensorEventListener {

	private SensorManager mSensorManager;
	private Sensor mAccelerometer;

	public static float TILT;

	public AccelerometerHelper(Activity oActivity) {

		mSensorManager = (SensorManager) oActivity.getSystemService(Context.SENSOR_SERVICE);

		if (mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
			mAccelerometer = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
			mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);


	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub


	public void onSensorChanged(SensorEvent event) {

		TILT = event.values[0];



After setting up the SensorManager we need to check whether our sensor (accelerometer) is available (l.21). If the size of the Sensor List would be 0 that would mean there is no sensor (accelerometer) available.

So in the next lines we setup our sensor called mAccelerometer and register your sensor and your listener.

  • registerListener(listener, sensor, rate)
    • The listener receives data from the sensor with changing rate rate.
    • rate
      • There are different delay rates available
        • delay between events in microseconds
  • TILT = event.values[0];
    • values[0]: Acceleration minus Gx on the x-axis
    • values[1]: Acceleration minus Gy on the y-axis
    • values[2]: Acceleration minus Gz on the z-axis

Now we are able to get the tilt of our android in the X-axis.

But before we can use the tilt function we have to assign a value to AccelerometerHelper.

  • In AndEngineTutorialActivity under onCreateEngineOptions() add
    • mAccelerometerHelper = new AccelerometerHelper(this);
  • Go back to Player and change the setVelocityX value to
    • setVelocityX(-AccelerometerHelper.TILT * 100)
  • Just to make it look a bit better we can add a slight rotation to our player ( just add this line directly under the SetVeclocity one).
    • setRotation(-AccelerometerHelper.TILT * 7);


You can add the jump function yourself, I will post the code for the jump in the next post.