Skip to content

Getting Started: Working with Motion

September 17, 2011

Okay, so now that we’ve taken a look at the first kind of input, touch, let’s look at the other: Acceleration!  Every Android machine includes an accelerometer that can measure movement of the device in 3 directions- assuming you are facing the device, the X axis tracks movement tilting to the left and right, the Y axis tracks movement tilting up and down, and the Z axis registers movement forwards and backwards.

  (Image from Google)

What this means for you as a developer is that, given the right code, you can detect any movement in any direction on the device.  So the next question is: how?

Including Accelerometer Input

Using the accelerometer is quite simple.  First things first: your app needs to include accelerometer input.  To make this happen, start by implementing it in your class definition like this:

public class myClass extends BaseGameExample implements IAccelerometerListener {

This bolded section tells your game that you will be using the accelerometer, so it should include the code that makes it listen for that input.

Enabling and Disabling the Accelerometer

There’s another thing concerning the Accelerometer that you need to know about: enabling and disabling it.  At some point (likely in the onLoadResources section of your code), you’ll need to enable the accelerometer in code.

		this.enableAccelerometerSensor(this);

If for some reason, you want to stop or start using the accelerometer temporarily, you can use the code:

		this.disableAccelerometerSensor();

Now, the next question is: how do we use it?

Reading Accelerometer Input

Once your activity is setup to implement IAccelerometerListener, include code like this in the base of your class:

	public void onAccelerometerChanged(final AccelerometerData myAccelerometerData) {
		mySprite.setPosition(mySprite.getX() + myAccelerometerData.getX(), mySprite.getY() + myAccelerometerData.getY());
		if (myAccelerometerData.getZ() >= 0) { mySprite.setVisible(true);}
		else {mySprite.setVisible(false);}
	}
And that's really all there is to it.  In this example, whenever the user moves the device, this code is called.  It moves mySprite along with the orientation of the device (on the X-Y axis, at least), simple as that.  It then sets the device to visible if the device is moved forwards, and invisible if moved backwards.  One thing users often do with this is to use the device to set the gravity relative to the accelerometer- for more information on how to do this, take a look at AndEngineGuides, or stay tuned- getting started with Physics is coming soon!  
With this, you have the tools needed for multiple activities- movement based on device orientation (such as in  doodle drop or a ball labrynth) or action when the device is shaken (such as rolling a die).

And with that, you should have the tools necessary to begin experimenting with the accelerometer!  Up next: Getting Started: Sound and Music

Advertisements
2 Comments
  1. First i love your guides and am very happy with these. So thanks for that.

    I wanted to tribute to the small script above. If you copy and paste like it that, the sprite is always in the upper left corner of the screen. You need to add the current Sprite coordinates to let it move. Like this:

    ship.setPosition(
    ship.getX() + myAccelerometerData.getX(),
    ship.getY() + myAccelerometerData.getY()
    );

    • Thanks Dennis! I’ll edit it in. I’m glad you’re enjoying the guides- I’m hoping to be able to put together a few more as soon as this semester of school is over!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: