Hi! I think I can see two problems in runOutMatrix:
I think you aren’t accumulating the parent’s position, only its scale. The child’s total position should be createVector(parentX + parentScale * childX, parentY + parentScale + childY), as the parent’s scale also applies to the position relative to the parent.
It looks like you’re only accumulating position/scale from the direct parent, but that parent may also have a parent. Likely you will need to make your function recursive to account for that.
Also one thing that might be of interest is the DOMMatrixReadOnly JavaScript class: DOMMatrixReadOnly It lets you keep track of a transform with multiple nested translations and scales and will also let you extend your code to include rotations too if you need.
It would let you do something like this:
class Thing {
globalTransform() {
let parentMatrix = new DOMMatrixReadOnly()
if (this.parent) {
parentMatrix = this.parent.globalTransform()
}
return parentMatrix
.translate(this.position.x, this.position.y)
.scale(this.scale)
}
runOutMatrix() {
push()
const m = this.globalMatrix()
applyMatrix(m.a, m.b, m.c, m.d, m.e, m.f)
// draw something here
pop()
for (const child of this.children) {
child.runOutMatrix()
}
}