Martin
Martin

Reputation: 2673

three js recalculate vector 3 to child object

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??

Upvotes: 0

Views: 345

Answers (2)

Falk Thiele
Falk Thiele

Reputation: 4494

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 

Upvotes: 0

WestLangley
WestLangley

Reputation: 104783

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

Upvotes: 1

Related Questions