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 1st


  • \$\begingroup\$ 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? \$\endgroup\$ – bornander Jan 27 '17 at 10:08
  • \$\begingroup\$ You know the documentation gives you a lead, right? \$\endgroup\$ – Vaillancourt Jan 27 '17 at 10:10
  • \$\begingroup\$ currentlly ,according to the device orientation. \$\endgroup\$ – pintu236 Jan 27 '17 at 10:10
  • \$\begingroup\$ I read it couple of times.I need a start to get this result. \$\endgroup\$ – 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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.