Meshes position with same Vector3 lerps only one Mesh

Lovely community,

I encountered a strange behaviour, when I set position of one mesh as position of another and then lerp it, it will only move one of the meshes. Unfortunately for me, it is even the wrong mesh thats position get lerped (group has to move not spawnpoint). Maybe someone knows from if it is a bug or anything else?

By the following lines the group-node position is set to spawnpoint-mesh position:

group.position = spawn.position; // behaviour is as expected by using clone()

This is how I lerp inside registerBeforeRender:

lerp(dT) {
        if(group.position.equals(destination)) {
            // remove commanded
        }
        else {
            BABYLON.Vector3.LerpToRef(group.position, destination, dT * speed, group.position);
        }
    }

Github:

Lerp-function:

If you don’t want all the group’s positions and the spawn position to share the same Vector3 object, then I would use clone() or copyFrom() instead of assigning the same object to all of them. Usually I would use copyFrom to copy the wanted values to the existing object rather than creating a new object with clone(). :slight_smile:

1 Like

You are right, clone() or copyFrom() would be a solution, like I posted above. Might switch like you say to copyFrom. I just wondered and wanted to share (if it is really a bug) why only one mesh (spawnpoint) would move, but maybe reference is lost somehow for unit group?

For me at least, it would be hard to look into without downloading and running the repo and looking over the project more… It would be better to create a small playground repro that’s easier to investigate IMO. :slight_smile:

I think like this you see it, strangely in PG spawn position is not applied at all to group:

1 Like

If you eg call markDirty() on each mesh after lerping their shared position then computeWorld matrix will know that each mesh need its world matrix updated. Otherwise position._isDirty is reset to false after the world matrix is computed for the first mesh so the next meshes’ world matrices are thought to be up to date when computeWorldMatrix is called for them. EG you could also fix it by calling computeWordMatrix(true) for each mesh after lerping them (edit: but I would call markDirty() instead like I showed below)…

2 Likes

I see. Good to know there is this functionality with _isDirty and markDirty() to control world matrix update. In my case copyFrom() seems to be best solution, because I don’t want both to move.

1 Like

Hey!
This is because in this case both objects share the same dirty flag which means that when one object computes his world matrix, it resets the dirty flags and thus the second object does not update his own matrix because he thinks he is not dirty

Here is a way to fix it (but honestly I would not recommend doing it this way):
Bug Vector3 Lerp Meshes Positions | Babylon.js Playground (babylonjs.com)

If you want to animate only one, the best option is then to clone after the animation:
Bug Vector3 Lerp Meshes Positions | Babylon.js Playground (babylonjs.com)

Edit: lol I did not see that @blake replied before me:) so all credits for him

3 Likes