binyan

Convert global coordinates to local coordinates

Recommended Posts

Given a vector expressed in global space and a mesh, how can I get the position of this vector within a mesh local space?

Or, alternatively, how can I get a parent position within a child local space?

 

Update:

Just figured it out... It is simply vector.subtract(Mesh.getAbsolutePosition()), right? 

 

Update2:

Ok it's not... Because it doesn't take in account the mesh rotation. 

Share this post


Link to post
Share on other sites

Think I came up with a solution:

 var m = new BABYLON.Matrix(); mesh.getWorldMatrix().invertToRef(m); var v = BABYLON.Vector3.TransformCoordinates(vector, m);

Is that right? What I did is simply an inverse of local->world transformation process.

Share this post


Link to post
Share on other sites

Hello !

This is very useful for those who want to transform a vector into a mesh's space.  But I don't know why you use Matrix.InvertToRef.

I understand better this function :

       var vector = new BABYLON.Vector3(100,0,0);
        var m = mesh1.getWorldMatrix();
        var v = BABYLON.Vector3.TransformCoordinates(vector, m);
       mesh2.position = v;

 

 

Share this post


Link to post
Share on other sites
17 hours ago, getzel said:

But I don't know why you use Matrix.InvertToRef.

Your example seems to be getting the global position given the local coordinates.

Binyan was getting the local coordinates given the global coordinates.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.