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?
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?
Share Improve this question asked Oct 16, 2014 at 9:15 konturkontur 5,2192 gold badges39 silver badges67 bronze badges 6-
Is
object.worldToLocal( vector )
what you are looking for? – WestLangley Commented Oct 16, 2014 at 9:33 -
@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 Commented Oct 16, 2014 at 10:24 - Can you provide a simple live example, such as a jsfiddle? – WestLangley Commented Oct 16, 2014 at 10:30
- See here: jsbin./datiyekaroti/1 – kontur Commented Oct 16, 2014 at 10:56
-
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 Commented Oct 16, 2014 at 19:49
1 Answer
Reset to default 8You 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