Search code examples
androidaccelerometer

Accelerometer start position


After I get the values from the accelerometer (on X and Y axis) I assign this values to xSpeed and ySpeed variables and then I move a ball according to this values. The problem is that I want at the start of the activity xSpeed and ySpeed to be 0 (the ball will not move until the phone is moved) whatever the accelerometer values are. I would like to do this sort of calibration.

For Y axis i tried this but is not working for all starting positions of the phone.

ball.setYSpeed(ySpeed-startAccelY);

where startAccelY is the start value of the accelerometer on Y axis and to ySpeed will be asigned the new values.


Solution

  • I would suggest you take the original starting value and subtract it from the current reading.

    For example you have a variable in you code:

    float initialPosition = 1.4; // 1.4 is just an example, you'd set it to the reading of the accelerometer
    

    Then when you apply the current accelerometer reading to your ball, you can subtract the initialPosition value to make the ball move. This way the ball will appear to stall at the beginning.

    I hope this helps.