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
-
- The listener receives data from the sensor with changing rate rate.
-
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.
mAccelerometerHelper is a SensorEventListener? in AndEngineTutorialActivity
Since registerListener(SensorEventListener listener, Sensor sensor, int rate) in this example “this” is the SensorEventListener.
You can always make your own SensorEventListener…
I had to post the code at http://pastebin.com/ZCuRAGLw because it wasn’t formatted the right way in the comments.
Of course now you have to change the SensorEventListener in line 23 from “this” to mySensorEventListener.
Thank you for the nice tutorials.
I wondered if you maybe could do a tutorial to do movement by touch tho. So for example a left and right movement by a touch move event.
Hope you have the time for it and are willing to do so. Thank you in advance for all the hard work you are putting into this 😀
Hey,
Do you mean that the play should move to the position you touched or just move to the left/right?
The class AccelerometerHelper has a problem with this line of code.
mSensorManager = (SensorManager) oActivity.getSystemService(Context.SENSOR_SERVICE);
It says that Context cannot be resolved to a variable.
Also, mAccelerometerHelper = new AccelerometerHelper(this); is giving me an error, cannot be resolved to a variable type.
Hey,
This coud be an error of eclipse.
Try Project → Clean , this always solves problems 😀
That didn’t work. The errors are still there.
You just need to import Context.
The line import android.content.Context; should be in the code.
No need to add mAccelerometer cause it is unussed, just type in new AccelerometerHelper(); to call it and it should work.
my Texture is still there, on center of the screen. It don’t movement. Could you explain for me! Thanks
Hi, I am a newbie to AndEngine. I am planning to make a game with AndEngine GLES2 Anchor. I would like to add a haptic feature into my game such as when user wants to confirm to Quit, he would till his phone to the left or right to mean YEs or No instead of clicking on the Yes or No buttons.
There are really few tutorials on Accelerometer so would any one tell me how to start? and Is my idea feasible?
I would be really grateful for your help
I was reading a tutorial which uses GLES1 and implements IAccelerometerListener to measure the movement of the device. But i found no tutorials for GLES2 , so I wonder whether the use of this IAccelerometerListener is the same in the new version.
Thank you a lot.
You are awesome Please provide more tutorials on andengine. I am novice and your help is appreciated Thanks.
hey thanks for this tut.. works fine .. but my game is in landscape , and the sensor works great on portrait … any idea how can i make it suitable for landscape game ?
thanks