-
Hello! I am trying out excalibur and have been looking through and experimenting with some of the examples. I really like it so far!
I just wanted to check if there is another simpler way of implementing removal of components/constraints when using another physics engine such as matterjs? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @Demitor, great question! The components have deferred removal, they won't be cleared until the end of the frame by default. The force parameter forces immediate removal 👍 The reason is sometimes immediate removal can cause some bugs across system lists In order for the removal to have an effect on the matterjs world, you'll need to wire the MatterJsSystem to listen for component removals and clean up the Matter.js world. I didn't do the cleanup in the sample originally unfortunately. BUT I've updated the sample but the gist is in the system we listen for the components removal and cleanup. Here is the sample! Press the "D" key to remove the constraint https://github.com/excaliburjs/sample-matterjs export class MatterJsConstraintComponent extends Component<'matterjs.constraint'> {
public readonly type = 'matterjs.constraint' as const;
constraint: Matter.Constraint;
removed$ = new Observable<MatterJsConstraintComponent>();
constructor(public constraintDef: Matter.IConstraintDefinition) {
super();
this.constraint = Matter.Constraint.create(constraintDef)
}
onRemove(previousOwner: Entity): void {
this.removed$.notifyAll(this);
}
} export class MatterJsSystem extends System<MatterJsBodyComponent | TransformComponent> {
...
override notify(msg: AddedEntity | RemovedEntity) {
// Add bodies to the matter world
if (isAddedSystemEntity(msg)) {
const matterJsComponent = msg.data.get(MatterJsBodyComponent);
if (matterJsComponent) {
Matter.Composite.add(this.matterEngine.world, matterJsComponent.matterJsBody);
}
// Optional component
const matterJsConstraint = msg.data.get(MatterJsConstraintComponent);
if (matterJsConstraint) {
Matter.Composite.add(this.matterEngine.world, matterJsConstraint.constraint);
// Listen for removal after we set up the component
matterJsConstraint.removed$.subscribe(() => {
Matter.World.remove(this.matterEngine.world, matterJsConstraint.constraint);
});
}
}
}
...
} |
Beta Was this translation helpful? Give feedback.
Hi @Demitor, great question!
The components have deferred removal, they won't be cleared until the end of the frame by default. The force parameter forces immediate removal 👍 The reason is sometimes immediate removal can cause some bugs across system lists
In order for the removal to have an effect on the matterjs world, you'll need to wire the MatterJsSystem to listen for component removals and clean up the Matter.js world. I didn't do the cleanup in the sample originally unfortunately. BUT I've updated the sample but the gist is in the system we listen for the components removal and cleanup.
Here is the sample! Press the "D" key to remove the constraint https://github.com/excaliburjs/sa…