最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Three.JS, changing the world position of a child 3D object - Stack Overflow

matteradmin11PV0评论

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.

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.

Share Improve this question asked Sep 22, 2012 at 21:06 sbadamssbadams 1831 gold badge2 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

EDIT: Answer updated.


You want to change the world position of a child object.

Assuming the scene has no transform applied, one way to do what you want is to use the following pattern:

scene.attach( child ); // detach from parent and add to scene

child.position.set( x, y, z );

parent.attach( child ); // reattach to original parent

The Object3D.attach( child ) method adds the child object, while maintaining the child's world transform.

three.js r.132

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

If use attach for setting position in animation loop for moving objects you can get objects shaking.

For me best solution is:

let newPosition = new THREE.Vector3(1,2,3);
let parentPosition = new THREE.Vector3();
childobject.parent.getWorldPosition(parentPosition);
childobject.position.set(
    newPosition.x-parentPosition.x,
    newPosition.y-parentPosition.y,
    newPosition.z-parentPosition.z
);

If you need position and rotation attach is the only way.

Post a comment

comment list (0)

  1. No comments so far