I am making a mobile car game, and as it is a serious game for upper limb rehabilitation. The mobile is attached to the forearm of the patient, hence the forearm movement will move and rotate the car. All i need is to prevent earth gravity from moving my object, I tried to subtract 1g from the acceleromete's reading in z- axis, but the movement did not go rightly. Any help?


You got close to one of the possible solutions. However, there are may possible solutions to your problem. I've described two of them. I hope you'll find at least one of them satisfactory.

  1. Subtracting from the z-axis only works when the phone is aligned perfectly to the world (perpendicular to gravity) since the accelerator is measuring Z relative to the phone, not the world. You'll need to figure out the orientation of the phone relative to the earth's gravity.


acceleration = [ax, ay, ay]  // accelerometer reading

q                            // quaternion corresponding to the orientation

gravity = [0, 0, -9.81]      // gravity on Earth in m/s^2

a_rotated = rotate(acceleration, q) // rotate the measured acceleration into
                                    // the Earth frame of reference

user_acceleration = a_rotated - gravity

Source: https://stackoverflow.com/questions/18252692/gravity-compensation-in-accelerometer-data

  1. However, you could also solve this issue in the game itself. If the car is driving on a flat surface, you could use a physics engine to push the car back onto the driving surface, or calculate at what height you should place the car at every point on the surface and then align the car to the angle of the surface (calculated by rotating the car to align with the surface normal).
  • 1
    \$\begingroup\$ I can't understand, can you clarify more please? Recently I used gyro.userAcceleration instead of accelerometer. It doesn't include gravity in readings, so when mobile is flat, the car is not moving. but the movement is not smooth nor real, any suggestions? \$\endgroup\$ – Maha Mohy Aug 11 '18 at 8:42

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.