Search code examples
androidarcoresceneform

Placing an object with a given compass bearing in ARCore


I'd like to place a north facing arrow into an ARCore world using sceneform. I am trying to understand the correct system of transformations to go from the phone's compass to sceneform's quaternions.


Solution

  • This was the code I used to solve the problem:

        //Get the phone's pose in ARCore
        Pose deviceOrientedPose = frame.getCamera().getDisplayOrientedPose().compose(
                Pose.makeInterpolated(
                        Pose.IDENTITY,
                        Pose.makeRotation(0, 0, (float)Math.sqrt(0.5f), (float)Math.sqrt(0.5f)),
                        dhelper.getRotation()));
        float[] devquat = deviceOrientedPose.getRotationQuaternion();
        //Get the phone's heading in relation to the real world
        float heading = compassListener.getBearing(); //Use ROTATION_VECTOR sensor
        Quaternion deviceFrame = new Quaternion();
        deviceFrame.set(devquat[0],devquat[1],devquat[2],devquat[3]);
        double[] rpy = quat2rpy(deviceFrame);
        //Rotate around y axis... rotation in this case is the desired rotation
        float rotAngle = ((360-rotation)+heading+(float)Math.toDegrees(rpy[1])+360))%360;
        Quaternion qt = Quaternion.axisAngle(Vector3.up(),rotAngle);
    

    Essentially the idea here is to acquire the device pose irregardless of orientation. Subesquently, I listen to the bearing as given by the ROTATION_VECTOR sensor. Given that AR-Core's co-ordinate system is right-handed, rotation in the AR-Core world does not follow the convention where we describe a heading as clockwise to North. (360-rotation)+heading essentially rotates the anchor to face north (via +heading), then it applies the rotation of the desired heading (360-heading). And applies this to the camera's current rotation (float)Math.toDegrees(rpy[1])+360