I am facing a challenge where I need to apply a global position, obtained from a WebVR controller, to a child object that is nested within multiple parent objects with transformations. The child object I want to update does not have any transformations applied directly to it:
Scene
¦
¦
Object (with transformations)
¦
¦
Object (no transformations)
¦
¦
Child (cube) (The target for applying the global position)
When I apply the global position to a cube that is a direct child of the scene, everything works correctly:
cube.position.copy(globalPosition);
However, my requirement is to apply this position to the child object. I attempted to convert the global position to a local one using the following code:
const worldToLocal = new THREE.Matrix4().getInverse(cube.parent.matrixWorld);
cube.position.copy(globalPosition);
cube.applyMatrix(worldToLocal);
Unfortunately, after applying these transformation, the cube no longer sits in the same position as it would if it were a child of the scene directly. I'm unsure how to address this issue and would greatly appreciate any guidance provided. Thank you!