Skip to content

Commit

Permalink
fix(Entities): Invalidate matrix cache when setting a world matrix (#874
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jespertheend authored Feb 29, 2024
1 parent 5c80527 commit b755919
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/core/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ export class Entity {
this.rot = rot;
this.scale = scale;
this._localMatrixDirty = false;

// Mat4.decompose() doesn't extract negative scales correctly right now.
// Because of this, it's possible for the world matrix of children not
// to get marked as dirty when the scale changes from -1,-1,-1 to 1,1,1 for example.
// To fix that, we manually mark them as dirty.
this._markWorldMatrixDirty();
}

get worldMatrix() {
Expand All @@ -469,10 +475,12 @@ export class Entity {

_onWorldMatrixChange() {
if (this._ignoreWorldMatrixChanges) return;
const {pos, rot, scale} = this._worldMatrix.decompose();
this._worldPos.set(pos);
this._worldRot.set(rot);
this._worldScale.set(scale);
if (!this.parent) {
this.localMatrix.set(this._worldMatrix);
} else {
const newLocalMatrix = this.parent.worldMatrix.clone().invert().multiplyMatrix(this._worldMatrix);
this.localMatrix.set(newLocalMatrix);
}
}

/**
Expand Down
66 changes: 66 additions & 0 deletions test/unit/src/core/Entity/transformations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,72 @@ Deno.test({
},
});

Deno.test({
name: "setting world matrix affects children",
fn() {
const root = new Entity("root");
const childA = root.add(new Entity("A")); // The entity we will be moving up
const childB = childA.add(new Entity("B")); // The entity we will reset (i.e. move down again)
const childC = childB.add(new Entity("C")); // The entity that should have the identity matrix

childA.worldMatrix.set(Mat4.createTranslation(0, 1, 0));
assertMatAlmostEquals(childB.worldMatrix, Mat4.createTranslation(0, 1, 0));
assertMatAlmostEquals(childC.worldMatrix, Mat4.createTranslation(0, 1, 0));

childB.worldMatrix.set(new Mat4());
assertMatAlmostEquals(childC.worldMatrix, new Mat4());
},
});

Deno.test({
name: "setting world matrix affects children when scaling negatively",
fn() {
// At the time of writing, decomposing negatively scaled matrices means the sign is lost.
// So Mat4.createScale(-1, 1, 1) and Mat4.createScale(1, 1, 1) both return a scale of (1,1,1) when calling decompose()
// As a result changing the scale of a Entity.worldMatrix doesn't cause the worldmatrices
// of children to get marked as dirty.
const root = new Entity("root");
const childA = root.add(new Entity("A")); // The entity we will be moving up
const childB = childA.add(new Entity("B")); // The entity we will reset (i.e. move down again)

childA.worldMatrix.set(Mat4.createScale(-1, 1, 1));
assertMatAlmostEquals(childB.worldMatrix, Mat4.createScale(-1, 1, 1));

childA.worldMatrix.set(new Mat4());
assertMatAlmostEquals(childB.worldMatrix, new Mat4());
},
});

Deno.test({
name: "setting world matrix affects local position",
fn() {
const root = new Entity("root");
const childA = root.add(new Entity("A")); // The entity we will be moving up
const childB = childA.add(new Entity("B")); // The entity we will reset (i.e. move down again)

childA.worldMatrix.set(Mat4.createTranslation(0, 1, 0));
assertVecAlmostEquals(childB.pos, [0, 0, 0]);

childB.worldMatrix.set(new Mat4());
assertVecAlmostEquals(childB.pos, [0, -1, 0]);
},
});

Deno.test({
name: "setting world matrix affects world position",
fn() {
const root = new Entity("root");
const childA = root.add(new Entity("A")); // The entity we will be moving up
const childB = childA.add(new Entity("B")); // The entity we will reset (i.e. move down again)

childA.worldMatrix.set(Mat4.createTranslation(0, 1, 0));
assertVecAlmostEquals(childB.worldPos, [0, 1, 0]);

childB.worldMatrix.set(new Mat4());
assertVecAlmostEquals(childB.worldPos, [0, 0, 0]);
},
});

Deno.test({
name: "Issue #203: Changing parent transformation shouldn't adjust local transformation from children",
fn() {
Expand Down

0 comments on commit b755919

Please sign in to comment.