AR Virtual Object (Arrow) is not getting along the source and destination line

138 Views Asked by At

I am new to ARCore android. I was implementing indoor navigation using ARCore android. I calculated angle of line joining source and destination (Lat Long) from True north from following code :

private void getBearing(LatLng sourceLatLng, LatLng destLatLng) {
    Double sourceLat = sourceLatLng.latitude / 180 * Math.PI;
    Double sourceLong = sourceLatLng.longitude / 180 * Math.PI;
    Double destLat = destLatLng.latitude / 180 * Math.PI;
    Double destLong = destLatLng.longitude / 180 * Math.PI;

    double y = sin(destLong - sourceLong) * cos(destLat);
    double x = cos(sourceLat) * sin(destLat) - sin(sourceLat) * cos(destLat) * cos(destLong - sourceLong);

    double tan2 = atan2(y, x);                
    headingAngle = tan2 * 180 / Math.PI;     
    if (bearingAngle < 0) {
        bearingAngle = bearingAngle + 360;
    }
}

Then calculated angle of device orientation from True North (Azimuth Angle) using accelerometer, magnetometer. I want to align user device along the line joining source and destination (Lat,Long). And then calculated destination azimuth as following :

val currentDestinationAzimuth = (headingAngle - currentAzimuth + 360) % 360

And rotated arrow by currentDestinationAzimuth angle to make it align with source and destination. But arrow is not getting along the source and destination. It is getting diverted from its expected placement (along source and destination). I googled a lot and made changes in codes but still struck at this point. Below is code for 3d Model Rendering.

if (this.headingAngle >= this.mAzimuth && this.headingAngle <= this.mAzimuth + err ) {
        Session session = arFragment.getArSceneView().getSession();
        Vector3 cameraPos = arFragment.getArSceneView().getScene().getCamera().getWorldPosition();
        cameraForward = arFragment.getArSceneView().getScene().getCamera().getForward(); 
        Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(1.0f));
        Pose pose = Pose.makeTranslation(position.x, -1.0f, position.z);
        Anchor anchor = session.createAnchor(pose);
        anchorNode = new AnchorNode(anchor);                                 
        anchorNode.setParent(arFragment.getArSceneView().getScene());
        Node node = new Node();
        node.setLocalScale(new Vector3(0.40f, 0.40f, 0.40f));
        node.setRenderable(modelRenderable);
        node.setParent(anchorNode);
        arFragment.getArSceneView().getScene().addChild(anchorNode);
          double currentDestinationAzimuth = 0;
            Quaternion q1 = node.getLocalRotation();
            System.out.println("diff world rot : " + node.getWorldRotation());
            float angleOfRotation = (float) (headingAngle - initialFacing)
            currentDestinationAzimuth = (initialHeading - bearingOfLatLong + 360) % 360;
            Quaternion q2 = Quaternion.axisAngle(new Vector3(0f, 1f, 0f), (float) currentDestinationAzimuth);
            node.setWorldRotation(Quaternion.multiply(q1, q2));

}

Am i doing something wrong somewhere? Please guide.

Thanks for any help...

0

There are 0 best solutions below