Hi!
Some of you already know that @labris and me are working on the YUKA - A JavaScript library for developing Game AI BabylonJS examples. After switching to BabylonJS 5 beta the Matrix.copyFrom method started to cause issues, our AI vehicles has stopped to move. @labris have found out that something has changed in alpha-48 and that the updateFlag is still the same after using the copyFrom method. We are heavily relying on this method in almost all the examples.
A quick inspection of the BJS 5 Matrix source code revealed, that the updateFlag is copied from the source matrix to the destination matrix.
To be more precise we are copying the matrix data from a YUKA.Matrix and it doesn’t have the updateFlag, obviously. If I update the updateFlag manually, it is working. EDIT: I made a mistake here explaining how it works. Actually I create a new BABYLON.Matrix instance at startup. I copy the values from YUKA.Matrix to the BABYLON.Matrix at every frame and I use the copyFrom method from the BABYLON.Matrix I created at startup (it’s the same instance) to the AI vehicle (TransformNode) BABYLON.Matrix.
Is this guys a bug or was it an intention to change the behaviour of this method? If it’s an intention is this ‘hack’ the proper way to mark the matrix ‘dirty’?
I tried to search on the forum but I didn’t find anything about this issue.
Won’t that PR result in the matrix with the copied values always being thought different from the source, even if nothing was changed after copying its values? IOT wouldn’t below always be true, even if the matrix values were still the same?
if (matrix.updateFlag !== copiedMatrix.updateFlag) {
Maybe the problem is calling toArray(entityMatrix.m), which alters the matrix directly without changing its updateFlag, so the update flag is always the same unless they change it manually?
Here’s the documentation for updateflag which seems to say that it can be used for that purpose thou:
“You can use it to speed the comparison between two versions of the same matrix.”
Or maybe I am misunderstanding it, but it seems that before the PR you could tell if a matrix’s values had been changed after copying them by comparing the two matrices’ update flags…In other words you could compare the matrix with the copied values to the matrix that it was copied from, testing for equality without having to compare all 16 values, by just comparing their flags.
The update flags will be different because a static/global seed is incremented, not each matrix’s flag? Currently, after copying the values the flags were equal until one or both matrices was changed, so one could test the flags instead of all 16 elements, unless I’m misunderstanding something?
public _markAsUpdated() {
this.updateFlag = Matrix._updateFlagSeed++;
Well yes you are correct. It is an interesting side case I was not thinking about. (I forgot about the global counter…I’m getting old).
The problem is that if flags are different it does NOT necessary means the matrices are different (from their values). So it is a risky model to promote the use of the flag to test for equality
If _markAsUpdated was made public and was called after modifying the matrix values directly then I think it would work, LOL I didn’t realize it was a side effect thou, it seemed intentional copying the flag value over that way so I made some misassumptions I guess.
I think we can safely say that if the updateFlag are the same, then the matrices have the same values. Having different updateFlags doesn’t mean they are same or different.