How do I get the global position of an Object3D inside another Object3D?
setup:
var parent = new THREE.Object3D(); parent.position.set(100, 100, 100); var child = new THREE.Object3D(); child.position.set(100, 100, 100); parent.add(child); scene.add(parent);
notes:
I thought that this would be the way to do it:
console.log(child.localToWorld(parent.position));
...but it gives me (100, 100, 100), not (200, 200, 200).
In threejs r89 you can just get the world position by Object3D. getWorldPosition .
localPosition is the position of the GameObject with respect to its parent object. transform. position is the position of the GameObject with respect to the root.
You can extract the world position like so:
var target = new THREE.Vector3(); // create once an reuse it child.getWorldPosition( target );
target
will contain the result.
EDIT: updated to three.js r.120
In threejs r89 you can just get the world position by Object3D.getWorldPosition
. I don't know when it was added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With