4

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

Dinesh Shingadiya
  • 988
  • 1
  • 8
  • 23
Jude Asiamah
  • 73
  • 1
  • 10

2 Answers2

3

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

Hermes
  • 2,828
  • 2
  • 14
  • 34
  • So what's the difference between .getLocalPosition() and .getWorldPosition() when you place a node in a plane? – Jude Asiamah May 13 '19 at 14:12
  • `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. – Hermes May 13 '19 at 14:15
  • 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) . If 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). – Hermes May 13 '19 at 14:17
  • Oh okay. My question might be broad but please help me out. After I'm done detecting the plane, place Andy on the center of plane. Imagine it is an empty square room, I place Andy in the middle in the room. Now, I want to change Andy's position to the right side of the room, do I have to use the `LocalPosition` or `WorldPosition`? – Jude Asiamah May 13 '19 at 14:28
  • If you are detecting a plane and using it as the parent for the model (Andy) 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. – Hermes May 13 '19 at 14:40
  • 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 consistant – Hermes May 13 '19 at 14:42
  • 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 – Hermes May 13 '19 at 14:43
  • Thank you very very much. I understand it now. – Jude Asiamah May 13 '19 at 14:49
  • Do I have to clear the old positions in the node and replace them with the new set of positions? For instance, after generating a random position from a function for the node, should I clear the `setLocalPosition()` to `setLocalPosition(0,0 0)` And replace them with what the function has provided. – Jude Asiamah May 25 '19 at 04:04
  • No you do not have to reset the position to (0,0,0). for example, you have a model at (1,1,1) and you want to move it to (2,2,2). If you set the local position to (0,0,0) the. Set again at (2,2,2) it will cause the model to “jitter” or jump as it’s translation changes to (0,0,0) then (2,2,2). – Hermes May 25 '19 at 12:05
  • Oh okay.. Thanks – Jude Asiamah May 25 '19 at 15:51
  • I've the position of the nodes in different positions. That's they'll have different `LocalPositions`. I created a button to assign the nodes to position (0,0,0) and this means they've to be in the same position. But it's not working ... Please any solution. – Jude Asiamah May 26 '19 at 19:23
0

This is the portion of the code that needs to change the position of the room. I tried .setLocalPosition() but it didn't change position of the model but .setWorldPosition() worked. Please any solution?

private void changeNodeDetails(float psX, float psY, float psZ,String fnName) { Vector3 furnPosition; furnPosition = new Vector3(psX,psY,psZ);

    this.newOptimizeName = fnName;
    this.psResults = furnPosition;

    List<Node> nodes = arFragment.getArSceneView().getScene().getChildren();

    Optional<Node> optionalNode = nodes.stream().filter(n->n.getName().equals(fnName)).findFirst();`

if(optionalNode.isPresent()){ ``Node node = optionalNode.get(); ``try{ ``Vector3 x = new Vector3(0,0,0); ``Node childNode = node.getChildren().get(0); ``childNode.setWorldPosition(furnPosition); ``node.removeChild(node.getChildren().get(0)); ```node.addChild(childNode); ```Log.i("Furniture Name",String.valueOf(childNode.getName()));

            ```Log.i("Furniture Position x,y,z",String.valueOf(childNode.getWorldPosition()));`

        ``}
        ``catch (IndexOutOfBoundsException ioobe){ ioobe.printStackTrace(); }

    }
Jude Asiamah
  • 73
  • 1
  • 10
  • Have you checked the localPosition before you try to change it to make sure you are not setting a similar value? For example, if the local position is (2,2,2) and you attempt to change it to the same value nothing will move. – Hermes May 28 '19 at 19:15
  • also...why do you delete the childNode only to add it on the next line? – Hermes May 28 '19 at 19:16