Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to move the ball as I move the device but I can not able to find how to use accelerometer in libGDX. I know that Gdx.input.getAccelerometerX() returns x coordinate but I don't know how to apply it to the body I have created.

share|improve this question
1  
I think you can just get the x and y and apply it to your body as a velocity or acceleration. To apply it as a velocity to your object you can just do Position.X = Position.X + Gdx.input.getAccelerometerX() * ElapsedTimeInSeconds –  OriginalDaemon Apr 25 '12 at 11:39
    
Basically I want to move a ball on the surface using acceleration so what I did is, I apply world gravity to getAccelerometerX and getAccelerometerY in render method but I found no movement in the ball during execution. –  Siddharth Apr 25 '12 at 13:49
2  
@Siddharth You need to work on your accept rate. 20% is low. –  Byte56 Apr 25 '12 at 14:17
add comment

2 Answers

LibGDX offers these three methods in the com.badlogic.gdx.Input interface:

  • float getAzimuth(): The azimuth is the angle of the device's orientation around the z-axis. The positive z-axis points towards the earths center.
  • float getPitch(): The pitch is the angle of the device's orientation around the x-axis. The positive x-axis roughly points to the west and is orthogonal to the z- and y-axis.
  • float getRoll(): The roll is the angle of the device's orientation around the y-axis. The positive y-axis points to the magnetic north pole of the earth.

All three return a value in degrees. They tell you what angle the phone's being held at in the world, so you should also be able to construct a plane in your game which the ball would be sitting on, and thus determine how the ball should roll. Do note these only map perfectly to the screen if the phone is facing north, so you'd need to use the Azimuth value to find the direction of the game's local 'north' (which points towards the top or the side of the phone).

From there you just implement the physics of a ball rolling down a hill, which would mean applying acceleration to the ball depending on the direction and steepness of the ramp it's on.

share|improve this answer
    
sir I got all the value zero on the device when I was debugging so no value found from the device so what special thing I need to do. –  Siddharth Apr 26 '12 at 5:11
    
I don't know what you're doing (or not doing), and in particular I've never worked with LibGDX before. At a guess, I'd say you're not actually reading from the device's accelerometer and there's something you need to do to begin accessing that information. –  Jonathan Hobbs Apr 26 '12 at 5:24
add comment

I have found an answer of my question after hardworking towards it. The solution is

cfg.useAccelerometer = true;

For using accelerometer we have to enable it in the android file that this I was missing

share|improve this answer
2  
Is that an answer to this question, or your question on my answer about getting values of zero? If it's a response to your question about getting values of zero, please don't post it as an answer to this question. –  Jonathan Hobbs Apr 26 '12 at 10:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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