# Getting direction from mesh

**URL:** <https://forum.babylonjs.com/t/getting-direction-from-mesh/45285>\
**Category:** Questions\
**Tags:** mesh\
**Created:** [October 29, 2023, 1:28pm UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285 "2023-10-29T13:28:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sevaseva](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@sevaseva](https://forum.babylonjs.com/u/sevaseva)\
**Post date:** [October 29, 2023, 1:28pm UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285/1 "2023-10-29T13:28:16Z")

</div>

Hi,

I have a mesh in a running babylonjs Scene (either imported by calling `SceneLoader.ImportMeshAsync` or created by a function like `CreateBox` - in case that matters).

I set its position and direction (which I am assuming is the orientation in the scene…) like this:

```auto
mesh.setAbsolutePosition(new Vector3(1, 2, 3));
mesh.setDirection(new Vector3(1, 0, 0));

```

I activated a GizmoManager in the scene for rotation and enabled gizmo on my mesh:

```auto
const m = new GizmoManager(scene);
m.rotationGizmoEnabled = true;
m.attachableMeshes = [mesh];

```

I see the mesh rendered on the canvas.

Let’s say that as a user, at runtime, I click the mesh, see the gizmo, and use gizmo to rotate that mesh _just very slightly_.

Now as I user I click a button that dumps this mesh’ `getDirection()` to console, and I want to see that the mesh’ orientation changed _just very slightly_… Here is where I have a problem:

First, of course, `getDirection()` has a required argument; I don’t know what to pass in there. All I want if to get back _the same_ (or slightly different if the mesh has been rotated slightly by the user) direction/orientation that I passed to that mesh previously by calling `setDirection()`. If I pass to `getDirection` some Vector3 values (say `new Vector3(1, 0, 0)` or `new Vector3(1, 0, 1)` or whatever), I do get some values returned by that `getDirection()`, but they are unexpected and I don’t know how to interpret them.

So my simple question is - how do I get the direction/orientation from the mesh, having previously set it using `mesh.setDirection(new Vector3(1, 0, 1))` ? Or maybe calling setDirection isn’t the way to specify the mesh’ orientation in the the scene… then what _is_ the way?..

If someone offers guidance on how I should be thinking about that `localAxis` that’s associated with mesh’ direction, I would also appreciate that greatly.

Thank you very much!  
Seva

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 30, 2023, 11:14am UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285/2 "2023-10-30T11:14:17Z")

</div>

`getDirection` is simply transforming the direction you pass in by the world matrix of the mesh, so you won’t get the axis you pass to `setDirection` this way (I’m not sure why `getDirection` is called this way).

You would need to invert the calculation done by `setDirection` to retrieve the axis you seek.

I tried to do it in this PG:

> **[Babylon.js Playground](https://playground.babylonjs.com/#30CMWI%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Look at the console, it prints the value you passed to `setDirection`, then the value generated by the local `getDirection` method.

It seems to work, but I don’t give any guarantee!

Note that it won’t work in the general case if you set a roll (rotation around the Z axis), because the calculation only takes into account pitch and yaw. So, if you use the gizmo, you won’t get the right axis if you rotate around Z.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 30, 2023, 11:18am UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285/3 "2023-10-30T11:18:48Z")

</div>

Regarding the `localAxis` passed to setDirection, you should think of it as the “deviation” of the mesh according to the natural orientation of the mesh, which is (0,0,1). So, if you pass (0,0,1), the orientation of the mesh won’t change. If you pass (1,0,1), the mesh will be reorientated so that the (0,0,1) axis matches (1,0,1), meaning the mesh will be rotated around Y “on the right”.

I think the PG I gave above will help you understand it: start with a (0,0,1) axis to see what is the original orientation of the cones when there’s no rotation applied (one cone is pointing to the +X direction and the other to +Y), then change the axis and see how it rotates the mesh.

---

<div class="post-metadata">

**Author:** ![sevaseva](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@sevaseva](https://forum.babylonjs.com/u/sevaseva)\
**Post date:** [November 1, 2023, 1:05am UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285/4 "2023-11-01T01:05:15Z")

</div>

Many thanks, @Evgeni_Popov

Your answer seems to answer the question as I posed it and attempts to explain the intuition behind `localAxis`.

It at also helped me to finally see that calling `setDirection` to just set mesh’ orientation/rotation in the scene is plain wrong, it’s a misuse of that (confusingly named and poorly documented) method.

// In my defense, I inherited, didn’t write the code that called setDirection; not that anyone cares.

In this particular case all I care about is setting the mesh’ orientation/rotation in the Scene and seeing it hanging there with the orientation that I set, and maybe later changing the orientation at runtime and querying the mesh to get its up-to-date orientation (no animation, no physics involved at the moment).

For doing that, is it fair to say that I should simply set and read [AbstractMesh#rotation](https://doc.babylonjs.com/typedoc/classes/BABYLON.AbstractMesh#rotation) and leave the `localAxis` (aka “direction”) of the mesh at its default value of (0,0,1)?..

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 1, 2023, 11:11am UTC](https://forum.babylonjs.com/t/getting-direction-from-mesh/45285/5 "2023-11-01T11:11:59Z")

</div>

Indeed, `mesh.rotation` is all you need if you want to update the orientation of the mesh (or `mesh.rotationQuaternion` if you want to use quaternions instead of Euler angles).

Note that there is no “mesh direction” per see: there’s no property for that. The `setDirection` method simply takes a vector and compute some values for the `rotation` property assuming the vector you pass is a “deviation” around the (0,0,1) vector.
