# How to use Libgdx Accelerometer?

I am developing a game with libgdx for android.I want use accelerometer for motion control.I am controlling a coin within screen bounds. I want to apply gravity according to device orientation as you can see in the image. I know i can use Gdx.input.getAccelerometerX()(or y,z) But i can't understand how to use to get desired result.

Here i have attached two sample image for simple understanding

• Does the gravity need to always point down to the earth or just down according to the device orientation? If you hold the device at a 45 degree angle, should gravity be pointing towards the corner of the screen? – bornander Jan 27 '17 at 10:08
• You know the documentation gives you a lead, right? – Vaillancourt Jan 27 '17 at 10:10
• currentlly ,according to the device orientation. – pintu236 Jan 27 '17 at 10:10
• I read it couple of times.I need a start to get this result. – pintu236 Jan 27 '17 at 10:12

Gravity is a type of acceleration, this is what the sensor gives you.

Acceleration is a vector value, because it has a direction. The length of the gravity vector is always around 9.81 becahue that's the average gravity (of course it can be different, for example on the Mount Everest you have a smaller gravitational force than on the bottom of the see)

If you want the ball, to always fall to the ground with an acceleration matching the gravity, then using the acceleration sensor is not enough. If you start waving the device around, then the acceleration will change. The accelerometer also doesn't register anything when the phone is freefalling. If you don't care about this, then you can skip the next paragraph.

You should instead use the orientation of the device, create a unit vector out of that and multiply it with 9.81.

Then, to apply this to an object, you need to increase it's velocity by this vector multiplied by the amount of time that's passed between calls (time should be in seconds, with 60 fps it should be around 0.016666 seconds)

• how can i get orientation of the point?I tried doing Gdx.input.getRotation() and check in render if(//device at 90) { – pintu236 Jan 27 '17 at 15:03
• but didn't get desired result. – pintu236 Jan 27 '17 at 15:18
• @DMGregory Read it again, that's what I'm doing – Bálint May 8 '17 at 13:36
• @DMGreg I think I brainfarted there, it should be "orientation of device" – Bálint May 9 '17 at 5:06