Skip to content

Commit

Permalink
Fixes RigidBody remove component (#5803)
Browse files Browse the repository at this point in the history
* Removing RigidBodyComponent now correctly destroys RigidBody. Fixes #4145

* Added check if rigid body exists
  • Loading branch information
marklundin committed Nov 8, 2023
1 parent 960b2e5 commit 1ccca06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/framework/components/rigid-body/component.js
Expand Up @@ -503,8 +503,12 @@ class RigidBodyComponent extends Component {
}

if (shape) {
if (this._body)
this.system.onRemove(entity, this);
if (this._body) {
this.system.removeBody(this._body);
this.system.destroyBody(this._body);

this._body = null;
}

const mass = this._type === BODYTYPE_DYNAMIC ? this._mass : 0;

Expand Down
10 changes: 2 additions & 8 deletions src/framework/components/rigid-body/system.js
Expand Up @@ -332,7 +332,6 @@ class RigidBodyComponentSystem extends ComponentSystem {
this.frameCollisions = {};

this.on('beforeremove', this.onBeforeRemove, this);
this.on('remove', this.onRemove, this);
}

/**
Expand Down Expand Up @@ -433,14 +432,9 @@ class RigidBodyComponentSystem extends ComponentSystem {
if (component.enabled) {
component.enabled = false;
}
}

onRemove(entity, component) {
const body = component.body;
if (body) {
this.removeBody(body);
this.destroyBody(body);

if (component.body) {
this.destroyBody(component.body);
component.body = null;
}
}
Expand Down

0 comments on commit 1ccca06

Please sign in to comment.