androidcameralibgdxorientationandroid-sensors

LibGDX camera rotation does not work on Android


I have the orientation values from Android device RotationVector sensor expressed in degress as Yaw, Pitch and Roll angles. I want to continuously update the LibGDX Perspective camera rotation based on these orientation values. My goal is to build a 3D compass.

In the render() method I am trying:

@Override
public void render() {
    ScreenUtils.clear(Color.BLACK, true);

    // do some rendering
    modelBatch.begin(cam);
    modelBatch.render(instance, lights);
    modelBatch.end();
    
    // calculate/get the orientation angles
    float Yaw = ...;
    float Pitch = ...;
    float Roll = ...;

    // update camera orientation
    cam.view.setFromEulerAngles(Yaw, Pitch, Roll);
    cam.update();
}

I see the continuously changing Yaw/Pitch/Roll values in the logcat on the Android device orientation change, but unfortunately my model (the 3D compass) does not rotate. What is wrong with the above code? Why the camera rotation does not work?


Solution

  • Thanks to @bornander I ended up with the below perfectly working code:

    @Override
    public void render() {
        // clear screen
        ScreenUtils.clear(Color.BLACK, true);
        
        // do some rendering
        modelBatch.begin(cam);
        modelBatch.render(instance, lights);
        modelBatch.end();
    
        // calculate Azimuth (Yaw), Pitch and Roll angles (in degress)
        float Azimuth = ...
        float Pitch = ...
        float Roll = ...
    
        // Transform the Forward vector (Vector3.Z) using a quaternion got from Azimuth/Pitch angles to a LookAt vector
        Quaternion rotCam = new Quaternion().setEulerAngles(Azimuth, Pitch, 0);
        Vector3 newDir = new Vector3(Vector3.Z).mul(rotCam).nor();
        cam.lookAt(newDir);
    
        // Transform the camera UP vector (Vector3.Y) using a quaternion got from previous LookAt vector and Roll angle
        Quaternion rotUp = new Quaternion(newDir, Roll);
        Vector3 newUp = new Vector3(Vector3.Y).mul(rotUp).nor();
        cam.up.set(newUp);
    
        // apply the above changes on camera
        cam.update();
    }