Jelajahi Sumber

Legacy Collision Detection Event Support

Allow for basic collision detection support without specific imposters.
Fixes otherImporters.length === 0 issue.
MackeyK24 8 tahun lalu
induk
melakukan
7c07a20a1d
1 mengubah file dengan 11 tambahan dan 2 penghapusan
  1. 11 2
      src/Physics/babylon.physicsImpostor.ts

+ 11 - 2
src/Physics/babylon.physicsImpostor.ts

@@ -330,13 +330,22 @@ module BABYLON {
             }
         }
 
+        /**
+         * Legacy collision detection event support
+         */
+        public onCollideEvent: (collider:BABYLON.PhysicsImpostor, collidedWith:BABYLON.PhysicsImpostor) => void = null;
+
         //event and body object due to cannon's event-based architecture.
         public onCollide = (e: { body: any }) => {
-            if (!this._onPhysicsCollideCallbacks.length) return;
             var otherImpostor = this._physicsEngine.getImpostorWithPhysicsBody(e.body);
             if (otherImpostor) {
+                // Legacy collision detection event support
+                if (this.onCollideEvent) {
+                    this.onCollideEvent(this, otherImpostor);
+                }
+                if (!this._onPhysicsCollideCallbacks.length) return;
                 this._onPhysicsCollideCallbacks.filter((obj) => {
-                    return (obj.otherImpostors.length === 0 || obj.otherImpostors.indexOf(otherImpostor) !== -1)
+                    return obj.otherImpostors.indexOf(otherImpostor) !== -1
                 }).forEach((obj) => {
                     obj.callback(this, otherImpostor);
                 })