The exotic bug with the Gizmo manager when midpoints rotation

Under some circumstances, objects that rotate with the gizmo stop transmitting their WorldTransform matrices.One of the circumstances is the opened debugging console in the browser (I checked in Opera and Chrome). If you open the example and rotate the cube with and without the console, you will notice that in the first case the rotation matrices and positions are reset to zero. This only happens when rotating through the edges midpoint. If you enable rotationGizmo and rotate with it, then everything is ok. This bug also appears in my project without the console, but the exact reason could not be found.
https://www.babylonjs-playground.com/#DEYAQ5#94

So! very reproducible. And very solvable :slight_smile:

First - you are using setInterval, which will run outside of the render loop. This is by all means not an error, but I would always recommend doing those kind of computations before or after render (using the observables or the available callbacks) so that you will have the guaranty that the data you are using is the right one.

Now, about the bug - you are using getWorldMatrix(), which will give you a cached (and sometimes incorrect) matrix, for performance reasons. The world matrix of each object is calculated right before rendering it (again, performance reasons), so it is guarantied to be correct AFTER render. and this is why this works:

https://www.babylonjs-playground.com/#DEYAQ5#95

If you want to have it at any other place (before render, for example), you will need to force-update the matrix, using computeWorldMatrrix(true):

https://www.babylonjs-playground.com/#DEYAQ5#96

or even using setInterval (notice that the interval has to be cleared in the playground, otherwise it will run over and over again in future scenes as well)

https://www.babylonjs-playground.com/#DEYAQ5#98

computeWorldMatrix is rather costly, so i wouldn’t recommend to do this on many objects. having said that, it is sometimes needed, and you should use it if it fits your usecase

1 Like

Thank you so much for such a detailed answer! It really helped me a lot :slight_smile:

1 Like