Hi, guys
I have a cube, and I change its position, so the vector will be change,
but when I open this, the x, y, z is different, I don’t know why?
and I dont’t know what is “isDirty:true” means, can someone explain ?
and how can I get the modify vector properly ?
thank you for reading.
Please give a playground example showing how you are changing the cubes position and obtaining the console log giving these results.
the _isDirty is an internal mechanism which I believe is a check during some processes to tell whether the vector3 is going to be changed. It can depend on when during the processes the browser logger picks up values as to the reading you get.
Try this to obtain values, as, I think, x, y, z will only be assigned when the change process has been completed .
//cube change of position code here
const x = cube.position.x;
const y = cube.position.y;
const z = cube.position.z;
console.log({x, y, z});
Ways to change the position vector
Other ways to modify a vector3.
Also looking at the source code for Vector3 can be super helpful. There we can see that the internal value is actually set right away and that the getter simply returns that internal value. The setter sets _isDirty to true to keep track of whether the vector has been changed, for example if you change a mesh’s position then Babylon will know that the mesh’s world matrix needs to be recomputed by checking this flag…
public get x() {
return this._x;
}
public set x(value: number) {
this._x = value;
this._isDirty = true;
}
Unfortunately it’s not uncommon for browsers to show the wrong property values when looking at an object in the console log. For example on Chrome on my MacBook I’m only able to see the current values, even if I log the object before changing them. Logging the values instead of the object is the only way to reliably see what the values were at the time I logged them AFAIK. Either using @JohnK’s solution of using the values to create a new object to log or just logging the values themselves, e.g. console.log(vector.x).
It’s not specific to Babylon thou. For example below incorrectly shows x having the value 1 even thou it was 0 when I logged the object.
let myObject = { x: 0, y: 0, z: 0 };
console.log(myObject);
myObject.x = 1;