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

**URL:** <https://discourse.threejs.org/t/problem-moving-an-object-along-raycast-axis-frustrating-yet-simple-vector-math/29504>\
**Category:** Questions\
**Tags:** webxr, coordinate-system, transform, math\
**Created:** [September 2, 2021, 6:16am UTC](https://discourse.threejs.org/t/problem-moving-an-object-along-raycast-axis-frustrating-yet-simple-vector-math/29504 "2021-09-02T06:16:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhazani](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mhazani/32/1141_2.png) [@mhazani](https://discourse.threejs.org/u/mhazani)\
**Post date:** [September 2, 2021, 6:16am UTC](https://discourse.threejs.org/t/problem-moving-an-object-along-raycast-axis-frustrating-yet-simple-vector-math/29504/1 "2021-09-02T06:16:25Z")

</div>

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:

```javascript
            let modelWorldPos = new Vector3();
            model.getWorldPosition(modelWorldPos);

            let controllerWorldPos = new Vector3();
            controller.getWorldPosition(controllerWorldPos);

            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)

---

<div class="post-metadata">

**Author:** ![thispatcher](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/thispatcher/32/28679_2.png) [@thispatcher](https://discourse.threejs.org/u/thispatcher)\
**Post date:** [April 11, 2023, 1:47am UTC](https://discourse.threejs.org/t/problem-moving-an-object-along-raycast-axis-frustrating-yet-simple-vector-math/29504/2 "2023-04-11T01:47:56Z")

</div>

bump. I too am trying to figure this out!

---

<div class="post-metadata">

**Author:** ![seanwasere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/seanwasere/32/20864_2.png) [@seanwasere](https://discourse.threejs.org/u/seanwasere)\
**Post date:** [April 11, 2023, 5:58am UTC](https://discourse.threejs.org/t/problem-moving-an-object-along-raycast-axis-frustrating-yet-simple-vector-math/29504/3 "2023-04-11T05:58:49Z")

</div>

This sounds exactly what my [GrabVR](https://sbcode.net/threejs/grabvr/) project does.  
See [Example 1](https://sbcode.net/threejs/grabvr-1/)  
Grab the object, then slide it close and far using joystick.

GitHub : [https://github.com/Sean-Bradley/GrabVR](https://github.com/Sean-Bradley/GrabVR)
