Problem moving an object along raycast axis (frustrating yet simple vector math)

Hello folks,

I am working on a problem involving WebXR controllers and an arbitrary object. When raycasting against the object and hitting the trigger, the object is made a child of my controller so that I can drag and translate it around (upon trigger release, the object goes back to being a child of the scene).

I’m trying to add a “force push / pull” functionality so that when joystick up or down is triggered, the object is moved away or towards the controller. A seemingly easy task, but the problems start since Object3D.translateOnAxis works in object space.

Say I have the following:

            let modelWorldPos = new Vector3();

            let controllerWorldPos = new Vector3();

            const dir = new Vector3();
            dir.subVectors(modelWorldPos, contWorldPos).normalize();

I would assume I now have a direction vector that points in the direction I actually want - from the controller to the model.
However, all attempts to convert it to the model’s object space using methods like const localDir = dir.transformDirection(model.matrix); and apply it in translateOnAxis - result in wrong directions; sometimes (when world 0 is aligned to object 0, presumably), the direction works as expect, but more often than not it is completely off, as if the coordinate space has not been converted yet.

What am I missing and/or is there a saner way to do this? Simply use the ray cast from the controller to the mesh (which I have access to)? How would I convert it to object space? Losing my mind and my hair here:). Thanks in advance.

(CC @Mugen87 who would definitely solve this in his sleep with both arms tied behind his back)

1 Like