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.

  • TYPE_ACCELEROMETER
    • A constant describing an accelerometer sensor type.
  • TYPE_ALL
    • A constant describing all sensor types.
  • TYPE_AMBIENT_TEMPERATURE
    • A constant describing an ambient temperature sensor type
  • TYPE_GRAVITY
    • A constant describing a gravity sensor type.
  • TYPE_GYROSCOPE
    • A constant describing a gyroscope sensor type
  • TYPE_LIGHT
    • A constant describing a light sensor type.
  • TYPE_LINEAR_ACCELERATION
    • A constant describing a linear acceleration sensor type.
  • TYPE_MAGNETIC_FIELD
    • A constant describing a magnetic field sensor type.
  • TYPE_ORIENTATION
    • This constant is deprecated. Use SensorManager.getOrientation() instead.
  • TYPE_PRESSURE
    • A constant describing a pressure sensor type.
  • TYPE_PROXIMITY
    • A constant describing an proximity sensor type.
  • TYPE_RELATIVE_HUMIDITY
    • A constant describing a relative humidity sensor type.
  • TYPE_ROTATION_VECTOR
    • A constant describing a rotation vector sensor type.
  • TYPE_TEMPERATURE
    • This constant is deprecated. Use Sensor.TYPE_AMBIENT_TEMPERATURE instead.

Source : http://developer.android.com/reference/android/hardware/Sensor.html

package com.PerleDevelopment.AndEngine.tutorial.helper;

import android.app.Activity;
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);
		}

	}

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

	}

	@Override
	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
        • SENSOR_DELAY_NORMAL
        • SENSOR_DELAY_UI
        • SENSOR_DELAY_GAME
        • SENSOR_DELAY_FASTEST
        • 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.