# Android Question LibGDX - How to calibrate accelerometer?

Discussion in 'Android Questions' started by jtare, Sep 24, 2017.

Im trying to set a new zero for the accelerometer and found this what seems to be the solution:
https://stackoverflow.com/questions/9705446/calibrating-3d-accelerometer-for-2d-game
But I couldn't translate it to B4a, methods like "Quaternion().setFromCross" are missing in the LibGDX library, it is possible to take this code and move it into B4A?

1. Capture a vector representing the neutral direction:
Code:
`Vector3 tiltCalibration =newVector3(Gdx.input.getAccelerometerX(),Gdx.input.getAccelerometerY(),Gdx.input.getAccelerometerZ());`
2. Transform this vector into a rotation matrix:
Code:
`publicvoid initTiltControls(Vector3 tiltCalibration ){Vector3.tmp.set(0,0,1);Vector3.tmp2.set( tiltCalibration ).nor();Quaternion rotateQuaternion =newQuaternion().setFromCross(Vector3.tmp,Vector3.tmp2 );Matrix4 m =newMatrix4(Vector3.Zero, rotateQuaternion,newVector3(1f,1f,1f));this.calibrationMatrix = m.inv();}`
3. Whenever you need inputs from the accelerometer, first run them through the rotation matrix:
Code:
`publicvoid handleAccelerometerInputs(float x,float y,float z ){Vector3.tmp.set( x, y, z );Vector3.tmp.mul(this.calibrationMatrix );x =Vector3.tmp.x;y =Vector3.tmp.y;z =Vector3.tmp.z;[use x, y and z here]...}`
For what I know, to work with Quaternion in LibGDX (B4A) first you need to declare a 4x4 matrix, you don't work directly with a variable of type Quaternion.
Something like:
Code:
`Dim Quaternion As lgMathMatrix4`