# Gizmo Get Distance in Local Space

## Recommended Posts

Hello Guys,

when I drag a rotated Mesh with the Gizmo in the direction of the rotation.

How do I calculate the Distance in Local Space (Drag X Distance)?

Thanks

Jonny

##### Share on other sites

I tried this so far:

this.parentMesh.computeWorldMatrix();

console.log('Parent: ' + this.parentMesh.position);
console.log('Origin: ' + this.origin);

const mat = new Matrix();

const diff = this.parentMesh.position.subtract(this.origin);

if (diff) {
console.log('Diff: ' + diff);

const quat = Helper.QuaternionToBabylon(this.assemblyItem.quaternion);

quat.toRotationMatrix(mat);

const trans = BABYLON.Vector3.TransformCoordinates(diff, mat);

console.log('Transformed: ' + trans);
}

But X Value is correct but also there is a difference in Y, even If i didnt drag Y.

##### Share on other sites

The difference in Y is very small though. If you rounded to, say, 8 decimal places it would = 0.

##### Share on other sites

Which gizmo are you using? Do you have playground code I can look at?

##### Share on other sites

On 8/31/2018 at 6:01 PM, babbleon said:

The difference in Y is very small though. If you rounded to, say, 8 decimal places it would = 0.

The diff is 4.6 and it should be 0 because in local space I only moved on X Axis.

On 8/31/2018 at 6:49 PM, trevordev said:

Which gizmo are you using? Do you have playground code I can look at?

The PositionGizmo with updateGizmoRotationToMatchAttachedMesh = true;

When I rotate the Mesh for 30 degrees and move it to with the x direction, then I want the distance I move on this axis.

I will create a PG and post it here.

##### Share on other sites

Hi @inxs, have a closer look at the number.. it's 4.6e-16, a very small number indeed!

Try this:

``console.log(.00000000000000046)``

##### Share on other sites

Ah ok thanks I didn't see that. But I dont think the calculation is correct.

I just want a short formula how to calculate the distance of two points in the localspace of my mesh?

##### Share on other sites

Ok already got the solution after browsing git repo

const tmpMatrix = new BABYLON.Matrix();
const localDelta = new BABYLON.Vector3();

this.parentMesh.computeWorldMatrix().invertToRef(tmpMatrix);
tmpMatrix.setTranslationFromFloats(0, 0, 0);
Vector3.TransformCoordinatesToRef(diff, tmpMatrix, localDelta);

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.