androidaugmented-realityarcoresceneformandroid-augmented-reality

How to change the position of a node in a plane?


I'm working on a project but I want to change the position of a 3D asset(example, Andy) when a button is tapped.

I've been using .getLocalPosition to get the position the asset but I don't know if it is the correct function.

If it is correct, I don't know whether .setLocalPosoition would work. Any help please


Solution

  • If your goal is to change the translation (position) of the model relative to its parent, you should use .setLocalPosition() passing in the Vector representing the new translation.

    public void setLocalPosition (Vector3 position)
    

    ARCore Documentation: https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/Node#setLocalPosition(com.google.ar.sceneform.math.Vector3)

    Edit (moving from comments in answer):

    LocalPosition refers to the translation of the node (model) relative to the parent node's origin, while WorldPosition refers to the translation relative to the world origin.

    For example:

    You have a world origin at (0,0,0) and within that scene there is a plane with translation of (1,1,1) . When you add a child to the plane and set it's local position to (1,1,1) that means it is placed in the scene relative to the plane's origin so the world position would be (2,2,2)

    If you are detecting a plane and using it as the parent for the model then you will want to use LocalPosition to move the model relative to the plane. Because of how you are parenting the model you would not want to use WorldPosition because you don't accurately know the world position.

    one thing you have to be concerned with when parenting a model to a plane that is detected with Plane detection, is keeping the Anchor reference. If the anchor reference to the plane gets lost then the model positioning will not be consistent.

    One work around would be to get the world translation from the detected plane and then add your model as a child of the scene using the plane's position. Then world and local position would be the same