4
votes

With a rotated object in my scene, how can I translate a global vector into the local space of that rotated object, so that they are rendered in the same spot of the global vector?

Take for example a cube that is rotated but at the global origin. Now I would like to render lines for each global dimension (x, y, z), but added as children to the rotated cube, not the global scene. How do I calculate and in three.js implement the rotation of those vectors so they will line up?

1
Is object.worldToLocal( vector ) what you are looking for?WestLangley
@WestLangley That was my first instinct as well, but that doesn't seem to get me what I want... so either I might be using it wrong, or that is not the solution. Assuming this does what I want, drawing a line along the following vector it should line up to the global X axis (which for me, it doesn't), should it not? var translatedAxis = myRotatedObject.worldToLocal(new THREE.Vector3(1, 0, 0));kontur
Can you provide a simple live example, such as a jsfiddle?WestLangley
Hey @WestLangley thanks for checking again. I ended up translating the Vector3 itself with applyMatrix4, but the parent.updateMatrixWorld(); // important! was the crucial missing piece. Feel free to add a reference to that as an answer and I'll gladly accept. :)kontur

1 Answers

8
votes

You want to add an object as a child of a parent object such that the world position and orientation of the child do not change.

You can do that using the following pattern:

parent.attach( object );

three.js r.107