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?
Something like:
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?
- Capture a vector representing the neutral direction:
B4X:Vector3 tiltCalibration =newVector3(Gdx.input.getAccelerometerX(),Gdx.input.getAccelerometerY(),Gdx.input.getAccelerometerZ());
- Transform this vector into a rotation matrix:
B4X: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();}
- Whenever you need inputs from the accelerometer, first run them through the rotation matrix:
B4X: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]...}
Something like:
B4X:
Dim Quaternion As lgMathMatrix4