Jerrina Paul
Jerrina Paul

Reputation: 31

How to update position of each child of a group in three.js

I have a group and when I drag and drop that group only group.position is changing. It's child's positions is still the initial values. They are not changing after drag and drop. How will I update the new position of each child?

group=new THREE.Group();
group.add(mesh1);`enter code here`
group.add(mesh2);
group.add(mesh3);

Suppose before translation group.postion:

position: THREE.Vector3 x: 0 y: 0 z: 0

group.children[0].position

x: -160.73611832689494 y: 56.06755614280701 z: 87.78444211930037

group.children[1].position

x: -29.662676970474422 y: 54.55312106758356 z: 14.629093836992979

after drag and drop:

group.postion

position: THREE.Vector3 x: -74.16755189596836 y: 0 z: -0.6893904394708485

group.children[0].position

x: -160.73611832689494 y: 56.06755614280701 z: 87.78444211930037

group.children[1].position

x: -29.662676970474422 y: 54.55312106758356 z: 14.629093836992979

pls give me a sloution.. How to update the matrix and get the new values of children's?

Upvotes: 1

Views: 10725

Answers (1)

SET001
SET001

Reputation: 11728

First of all - there are no more THREE.Group in latest THREE.JS revisions (r71). However you can use THREE.Object3D instead to group your objects:

var group=new THREE.Object3D();
group.add(box);
group.add(sphere);
scene.add(group);

Next, since you are making translations for group - local position of objects (inside that group) have not changed. If you need to get global position of child object you can get it like this:

var vector = new THREE.Vector3();
vector.setFromMatrixPosition( box.matrixWorld );

or simpler:

box.matrixWorld.getPosition();

here is a simple example at plnkr

Upvotes: 3

Related Questions