0

I have Vector3 in position.

I have scene childs:

planet = new THREE.Object3D();
city= new THREE.Object3D();
street= new THREE.Object3D();

scene.add(planet);
planet.add(city);
city.add(street);

how can i get correct world position of vector in each object(planet,city and street individually), with respect to position,scale and rotation of each object..

i try planet.worldToLocal(new THREE.Vector3( vector.x,vector.y,vector.z ) but it seem to work only from global to child object, not for recalculating from planet to city.

example:

planet.position.set(10,0,0);
city.position.set(10,0,0);
street.position.set(10,0,0);

if myVector is new THREE.Vector3(0,10,0), i need to get the same position in street object, that should be (30,10,0) how to get it by THREE functions??

Martin
  • 2,575
  • 6
  • 32
  • 53

2 Answers2

1

You can get the world position of an object like so:

var position = object.getWorldPosition(); // creates a new THREE.Vector3()

or

var position = new THREE.Vector3();
object.getWorldPosition( position ); // reuses your existing THREE.Vector3()

Note: If you have modified the position/rotation/scale of any of the objects in the hierarchy since the last call to render(), then you will have to force an update to the world matrices by calling scene.updateMatrixWorld(). The renderer calls this for you, otherwise.

three.js r.71

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • But i have Vector3 and i need local position of this vector in street object. Which is relative to city relative to planet, relative to world. That means i have world vector value and need relative to child of child of child. – Martin Jul 12 '15 at 11:06
0

I guess you want to use Vector3.add in combination with getWorldPosition():

planet.position.set(10,0,0);
city.position.set(10,0,0);
street.position.set(10,0,0);

myVector = new THREE.Vector3( 0, 10, 0 ); 
myVector.add( street.getWorldPosition() ); 
console.log( myVector ); // 30, 10, 0 
Falk Thiele
  • 4,424
  • 2
  • 17
  • 44
  • 1
    this works only if object have rotation 0,0,0 and fixed scale – Martin Jul 12 '15 at 13:45
  • There is also ```getWorldDirection()```. But maybe you want to consider the matrices for that, see http://stackoverflow.com/a/15098870/4977165 – Falk Thiele Jul 12 '15 at 14:14