Getting world position of child

Hello
I am pretty new at three and i’m having a problem in my app. I have a THREE.Group and in that group objects with changed position. When app renders and the group moves, everything looks nicely, but now I need to get the position of one of the children in order to create another (independant, not another child) object at that exact position. Here is somewhat simple example of my code.

var obj1 = new THREE.Mesh(someGeometry, someMaterial);
obj1.translateY(1.5); obj1.translateZ(2.5);
var obj2 = new THREE.Mesh(someGeometry, someMaterial);
obj2.translateY(-5); obj2.translateZ(2.5);
var obj3 = new THREE.Mesh(someGeometry, someMaterial);
obj3.translateY(3); obj3.translateZ(3);

var myGroup = new THREE.Group();
myGroup.add(obj1, obj2, obj3);
myGroup.position.set(somex, somey, somez);
scene.add(myGroup);

someOtherObject.position.set(?, ? , ?);

Now, if I want to create some other object and place it at the position of obj3, then how do I do it? myGroup.children[2].position gives me only x: 0, y: 3, z: 3, even if the group had already moved.

I have looked at many questions and tried multiple answers, but they never seemed to work correctly. Functions either no longer existed or returned position ( 0 , 0 , 0 ) or something else completely.
What would be the correct and most optimal solution as of right now? (three R89)
Thank you in advance

2 Likes

You can use Object3D.getWorldPosition() to get the position of obj3 in world space.

obj3.getWorldPosition( someOtherObject.position );

Notice how the position vector of someOtherObject is applied as a parameter. This memory-friendly approach avoids the instantiation of a result object. The position data are directly written to the someOtherObject.position vector.

3 Likes

Thank you for your help :slight_smile:

@Mugen87 How to get the world position of Child_C in Child_A?

If you want to move Child_A to the world position of Child_C you would do:

Child_C.getWorldPosition(Child_A.position);

Note: Transforming Child_A will also transform all descendants and thus Child_C.

However, using getWorldPosition() on Child_C is the correct method for receiving its position in world space.

1 Like