Android Question LibGDX - How to calibrate accelerometer?

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

  1. jtare

    jtare Active Member Licensed User

    Im trying to set a new zero for the accelerometer and found this what seems to be the solution:
    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:
      Vector3 tiltCalibration =newVector3(Gdx.input.getAccelerometerX(),Gdx.input.getAccelerometerY(),Gdx.input.getAccelerometerZ());
    2. Transform this vector into a rotation matrix:
      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:
      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:
    Dim Quaternion As lgMathMatrix4
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice