Search code examples
androidandroid-studioaugmented-realityarcoresceneform

Rendering multiple spheres by taping on a detected plane in ARCore


I am trying to make an application where I can render multiple spheres with on tap on a plane in a certain layout. I want it to be in the shape of a certain molecule, the spheres being the composing atoms. I need every individual sphere to be separate entity (separate node) so I can add OnTouchListeners to them and after it to make bonds between selected spheres. I am a little stuck. Does someone have an idea how can I approach this? I am working in android studio with Java and using ARCore and Sceneform.


Solution

  • You can add renderables and anchors when you detect a tape event, or when the user presses a button.

    So long as you know the relative positions that you want to add them, you can set the position for each node you add individually by setting a Pose translation.

    See below an example of adding a node 1M in front of the centre point of the preview screen:

                // Place the anchor 1m in front of the camera. 
                Frame frame = arFragment.getArSceneView().getArFrame();
                Session session = arFragment.getArSceneView().getSession();
                Anchor newMarkAnchor = session.createAnchor(
                            frame.getCamera().getPose()
                                    .compose(Pose.makeTranslation(0, 0, -1f)) //This will place the anchor 1M in front of the camera
                                    .extractTranslation());
                AnchorNode addedAnchorNode = new AnchorNode(newMarkAnchor);
                addedAnchorNode.setRenderable(andyRenderable);
    

    You can add multiple AnchorNodes with different renderables, and apply a different translation for each to position it where you want.

    The documentation for Pose.makeTranslation is here: https://developers.google.com/ar/reference/java/com/google/ar/core/Pose#makeTranslation(float,%20float,%20float)

    You can check on any touch event to see if any individual renderable has been touched and you can add lines between nodes also - see this repo for an example of detecting a renderable being touched and drawing a line between renderables:

    Note the above repository used the older release of Sceneform which has been deprecated, but the same principles still apply.