
You don't clean the world properly, that's why you collect subsystems and entries in the collision matrix etc.
However, you can easily add a method in World:
clear() {
const {bodies, constraints, solver, collisionMatrix, subsystems} = this;
// Remove current bodies
while (bodies.length) {
this.removeBody(bodies.pop());
}
// Remove all constraints
while (constraints.length) {
this.removeConstraint(constraints[0]);
}
solver.removeAllEquations();
collisionMatrix.matrix.length = 0;
subsystems.length = 0;
}
If this project weren't dead and written in JavaScript, I would have made a PR, but I just want to inform others about this leaky code.