binyan

Mesh.getAbsolutePosition

Recommended Posts

In this example I set a parent to the ground and then rotate it. I would expect that the absolute position of the ground would change but it doesn't... The weirder thing is that getAbsolutePosition returns always the same vector - the vector it returned first time I called it. For instance in this example the absolute position does change according to the rotation but after I get an absolute position for the first time it doesn't change.

Am I missing something?

Share this post


Link to post
Share on other sites

Thanks, Deltakosh. Checked it out and it works now. Just a little correction - I should force the both world matrices evaluation (the parent's one and the child's one). Otherwise it doesn't work.

If I understood well it will be automatically evaluated if this code will run in rendering loop, right?

Share this post


Link to post
Share on other sites

I ran into the same problem where i am trying to get the absolute position (or more generally, the world matrix components) of a mesh deep down in the hierarchy of a scene *before* any rendering happens. Seems like what i need to do is to go up the hierarchy and do computeWorldMatrix(true) for each mesh in the ancestry as well.

Deltakosh, might be worthwhile to consider a scene.computeWorldMatrixForAllMeshes (force)

Share this post


Link to post
Share on other sites

Here's some code i wrote to make sure that the world matrix is up-to-date (outside of rendering loop). Both these seem like nice utilities to add to BJS classes. I am new, so passing on code for now..

 

/* Get a node's parent hierarchy */
getHierarchy: function ( node, includeThis ) {
    var hierarchy = [];
    if ( includeThis )
        hierarchy.push( node );
    var item = node.parent;
    while ( item ) {
        hierarchy.push( item );
        item = item.parent;
    }

    return hierarchy;
}
,

/* Compute world matrix for node and its parent hierarchy */
computeWorldMatrix: function ( node, force, hierarchy ) {

    if ( hierarchy ) {
        var nodes = this.getHierarchy( node );
        for ( var n=nodes.length-1; n >= 0; n-- )
            nodes[n].computeWorldMatrix( force );
    }

    node.computeWorldMatrix( force );
}

Share this post


Link to post
Share on other sites

Note that i had to do this only because i was trying to get the world matrix of a mesh deep down in hierarchy BEFORE any rendering starts. (Wanted to set the camera position/rotation to that of the "eye" of the avatar). When i didnt do computeWorldMatrix(force) for the hierarchy, it was only returning the local matrix. After i did the above code, it worked.

Share this post


Link to post
Share on other sites

Hi, sorry for bumping up this old thread, but I think its not finished yet.

I have the same problem like Srisub. It occurs when I use computeWorldMatrix(true) inside the createScene function ( not in the runRenderLoop ). 

The result of the computeWorldMatrix looks wrong ( expect  {X:3 Y:1 Z:0}, but get {X:1 Y:0 Z:0} )

If I use the code from Srisub it works.

 

Here is a Playground where I can reproduce the problem.

http://www.babylonjs-playground.com/index.html#1HMKOS#3

@Deltakosh maybe you can have a look on it

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.