Search code examples
javaandroidandroid-studioaugmented-realityarcore

ARCore virtual object movement


So I started working with ARCore in Android Studio in Java, and I tested their demo HelloAR, which works.

Now I want to add simple thing such as move the object to scroll direction.

In TapHelper I need to add onScroll for GestureDetector

@Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        }

But that is where I am stuck now, the virtual object is rendered based on anchor? So do I need to update the anchor position or the position of the virtual object? And how do I do that, the anchor does not have any way of updating its position, do I destroy it and create new one, or did I miss anything?


Solution

  • If you just want to rotate the renderable itself you don't need to create a new anchor, but if you want to move the renderable to a new place in the 'world' then the standard approach, at this time, seems to be to delete the anchor and recreate it.

    Here is an example of how you might do this:

    private AnchorNode moveRenderable(AnchorNode myAnchorNodeToMove, Pose newPoseToMoveTo) {
            //Move a renderable to a new pose
            if (myAnchorNodeToMove != null) {
                arFragment.getArSceneView().getScene().removeChild(myAnchorNodeToMove);
            } else {
                Log.d(TAG,"moveRenderable - myAnchorNode was null");
                return null;
            }
            Frame frame = arFragment.getArSceneView().getArFrame();
            Session session = arFragment.getArSceneView().getSession();
            Anchor myAnchor = session.createAnchor(newPoseToMoveTo.extractTranslation());
            AnchorNode newMyAnchorNode = new AnchorNode(myAnchor);
            newMyAnchorNode.setRenderable(andyRenderable);
            newMyAnchorNode.setParent(arFragment.getArSceneView().getScene());
    
            return newMyAnchorNode;
        }
    

    The above is modified from a working example to make it more readable here - full source is here: https://github.com/mickod/LineView