Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the global/world position of a child object?

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).

like image 233
TunnelVision Avatar asked Feb 26 '13 20:02

TunnelVision


People also ask

How do you get the world position in three Js?

In threejs r89 you can just get the world position by Object3D. getWorldPosition .

What is the difference between transform position and transform localPosition?

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.


2 Answers

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

like image 170
WestLangley Avatar answered Sep 23 '22 15:09

WestLangley


In threejs r89 you can just get the world position by Object3D.getWorldPosition. I don't know when it was added.

like image 40
zwcloud Avatar answered Sep 22 '22 15:09

zwcloud