|
@@ -21,7 +21,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
private _fixedTimeStep: number = 1 / 60;
|
|
|
private _cannonRaycastResult: any;
|
|
|
private _raycastResult: PhysicsRaycastResult;
|
|
|
- private _removeAfterStep = new Array<PhysicsImpostor>();
|
|
|
+ private _physicsBodysToRemoveAfterStep = new Array<any>();
|
|
|
//See https://github.com/schteppe/CANNON.js/blob/gh-pages/demos/collisionFilter.html
|
|
|
public BJSCANNON: any;
|
|
|
|
|
@@ -55,11 +55,15 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
|
|
|
public executeStep(delta: number): void {
|
|
|
this.world.step(this._fixedTimeStep, this._useDeltaForWorldStep ? delta : 0, 3);
|
|
|
- if (this._removeAfterStep.length > 0) {
|
|
|
- this._removeAfterStep.forEach((impostor) => {
|
|
|
- this.world.remove(impostor.physicsBody);
|
|
|
+ this._removeMarkedPhysicsBodiesFromWorld();
|
|
|
+ }
|
|
|
+
|
|
|
+ private _removeMarkedPhysicsBodiesFromWorld(): void {
|
|
|
+ if (this._physicsBodysToRemoveAfterStep.length > 0) {
|
|
|
+ this._physicsBodysToRemoveAfterStep.forEach((physicsBody) => {
|
|
|
+ this.world.remove(physicsBody);
|
|
|
});
|
|
|
- this._removeAfterStep = [];
|
|
|
+ this._physicsBodysToRemoveAfterStep = [];
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -78,6 +82,9 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
}
|
|
|
|
|
|
public generatePhysicsBody(impostor: PhysicsImpostor) {
|
|
|
+ // When calling forceUpdate generatePhysicsBody is called again, ensure that the updated body does not instantly collide with removed body
|
|
|
+ this._removeMarkedPhysicsBodiesFromWorld();
|
|
|
+
|
|
|
//parent-child relationship. Does this impostor has a parent impostor?
|
|
|
if (impostor.parent) {
|
|
|
if (impostor.physicsBody) {
|
|
@@ -174,7 +181,9 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
this.world.removeEventListener("postStep", impostor.afterStep);
|
|
|
|
|
|
// Only remove the physics body after the physics step to avoid disrupting cannon's internal state
|
|
|
- this._removeAfterStep.push(impostor);
|
|
|
+ if (this._physicsBodysToRemoveAfterStep.indexOf(impostor.physicsBody) === -1) {
|
|
|
+ this._physicsBodysToRemoveAfterStep.push(impostor.physicsBody);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public generateJoint(impostorJoint: PhysicsImpostorJoint) {
|