Sfoglia il codice sorgente

do not automatically call forceUpdate for a physics impostor when setting scaling on a mesh

Trevor Baron 6 anni fa
parent
commit
d7126ba7fb

+ 2 - 0
dist/preview release/what's new.md

@@ -72,5 +72,7 @@
 - Only cast pointer ray input when pointer is locked in webVR ([TrevorDev](https://github.com/TrevorDev))
 - Avoid using default utility layer in gizmo manager to support multiple scenes ([TrevorDev](https://github.com/TrevorDev))
 - Fix bug when adding and removing observers in quick succession ([sable](https://github.com/thscott))
+- Cannon and Ammo forceUpdate will no longer cause an unexpected exception ([TrevorDev](https://github.com/TrevorDev))
 
 ## Breaking changes
+- Setting mesh.scaling to a new vector will no longer automatically call forceUpdate (this should be done manually when needed) ([TrevorDev](https://github.com/TrevorDev))

+ 0 - 3
src/Meshes/abstractMesh.ts

@@ -833,9 +833,6 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
 
     public set scaling(newScaling: Vector3) {
         this._scaling = newScaling;
-        if (this.physicsImpostor) {
-            this.physicsImpostor.forceUpdate();
-        }
     }
 
     // Methods

+ 1 - 0
src/Physics/Plugins/ammoJSPlugin.ts

@@ -468,6 +468,7 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                 impostor._pluginData.toDispose.forEach((d: any) => {
                     this.bjsAMMO.destroy(d);
                 });
+                impostor._pluginData.toDispose = [];
             }
         }
     }

+ 15 - 6
src/Physics/Plugins/cannonJSPlugin.ts

@@ -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) {