7
votes

So basically I have a child object3D of a group Object3D, while the child object's [x,y,z] coordinates are shown relative to the object space of the parent object, I want to change the location of the child object within the 3D space. So first I get the child object's position relative to the world space.

var wrld_pos = childobject.matrixWorld.multiplyVector3(new THREE.Vector3);

This returns a three element vector of the child's position within the world space. Now I wish to set the position my self. So I create a three element vector.

var new_pos = THREE.Vector3();
new_pos.x = 1;
new_pos.y = 2;
new_pos.z = 3;

childobject.matrixWorld.setPosition(new_pos);

Provided the function definition for setPosition, it essentially it sets the last three elements of the object's world matrix to the values of the vector passed as an argument, thus changing the location of the object in world space. And to make sure that the matrix updates after these changes, I call the following functions.

childobject.matrixWorldNeedsUpdate = true;
childobject.updateMatrixWorld();

Now upon inspecting the new world matrix of the object I noticed that the setPosition function did nothing, nothing at all.

Why? If real code examples are needed, I will provide them. But the above should represent the application domain and syntax I used very accurately.

2

2 Answers

9
votes

In three.js, it is best just to call object.position.set( x, y, z ), object.rotation.set( ... ), and object.scale.set( ... ), and not to mess with the object.matrix or object.matrixWorld directly, unless you really know what you are doing.

What is happening behind the scenes is this:

The default value of object.matrixAutoUpdate is true.

Hence, in the render() function, object.matrixWorld is being updated by the current value of object.postion, which you did not change. So, to you, it looks like nothing happened.

0
votes

This answer might help with the case. Remove child from parent, change normal position, then attach again.