Diogo Gomes
Diogo Gomes

Reputation: 2388

Keep object rotation based on parent/other object coordinate system in Three.js?

I'm trying to keep the rotation of the child object the same as it was before being added to a parent (which is the camera).

I'm using Three.js' worldToLocal to keep position but the rotation changes because of the parent's position and rotation.

Before and after "pick up":
Screenshot of the cube before pickup. Screenshot of the cube after pickup, position is the same but rotation changed.

Notice on how the cube keeps position (thanks to worldToLocal) but the rotation changes.

Edit: I want to be able to set the initial rotation of the cube the same it had before being added to a parent. After that I want the child to act normally and rotate/translate like the parent does.

Upvotes: 3

Views: 2882

Answers (1)

WestLangley
WestLangley

Reputation: 104763

To remove a child from a scene and add it to a new parent, while maintaining the child's world position and orientation, do this:

parent.attach( child );

Note: If the parent is the camera, you have to remember to add the camera to the scene:

scene.add( camera );

It is currently not a requirement that the camera is part of the scene.

three.js r.109

Upvotes: 9

Related Questions