user3648721
user3648721

Reputation: 157

ThreeJS copy one mesh world position rotation to another

I have 3 meshes: cube(a), tempSphere(b), m_mesh(c). I have mesh a and b in a group, a as parent.

I set location rotation and position of the group, a and b should update itself. Now I would like to set c to the current position and rotation of b. This is what I have and doesn't work

const cube: Mesh = this.createCube(size, objectColor);
group.add(cube);
const homePos: THREE.Vector3 = this.getHomePosition(size, homePosition);
const tempSphere = this.createHomePosition(cube, homePosition);
group.add(tempSphere);

// set local position of group
group.position.x = meshPosition.x;
group.position.y = meshPosition.y;
group.position.z = meshPosition.z;
// set local rotation of group
const rad = rotationZ * Math.PI / 180;
group.rotation.z = rad;

// trying to update world matrix of group and tempSphere
group.updateMatrixWorld(true);
tempSphere.updateMatrix();
tempSphere.updateMatrixWorld(true);
tempSphere.updateWorldMatrix(true, true);
    
// get world matrix of tempSphere
let worldRotQuat: THREE.Quaternion;
let worldPos: THREE.Vector3;
tempSphere.getWorldQuaternion(worldRotQuat);
tempSphere.getWorldPosition(worldPos);

// set world matrix of tempSphere to outside mesh
if (this.transScaleTool.m_mesh !== null) {
  this.transScaleTool.m_mesh.matrixWorld.setRotationFromQuaternion(worldRotQuat);
  this.transScaleTool.m_mesh.matrixWorld.setPosition(worldPos);
}

Upvotes: 1

Views: 664

Answers (1)

Mugen87
Mugen87

Reputation: 31026

It should be the following code pattern (assuming c has no rotated parent):

b.getWorldQuaternion( c.quaternion );

Object3D.getWorldQuaternion() automatically updates the world matrix of b so there is no need of manual update calls.

Upvotes: 2

Related Questions