浏览代码

Merge pull request #3201 from RaananW/cleanup

Cleanup
Raanan Weber 7 年之前
父节点
当前提交
3e68c65d9e
共有 3 个文件被更改,包括 36 次插入89 次删除
  1. 1 0
      .gitignore
  2. 0 13
      package-lock.json
  3. 35 76
      src/Physics/babylon.physicsHelper.ts

+ 1 - 0
.gitignore

@@ -166,3 +166,4 @@ localDev/src/*
 /dist/preview release/babylon.custom.js
 /dist/preview release/babylon.custom.js
 /dist/preview release/babylon.custom.max.js
 /dist/preview release/babylon.custom.max.js
 /localDev/src/index.js
 /localDev/src/index.js
+package-lock.json

+ 0 - 13
package-lock.json

@@ -1,13 +0,0 @@
-{
-    "name": "babylonjs",
-    "version": "3.1.0-beta5",
-    "lockfileVersion": 1,
-    "requires": true,
-    "dependencies": {
-        "cannon": {
-            "version": "0.6.2",
-            "resolved": "https://registry.npmjs.org/cannon/-/cannon-0.6.2.tgz",
-            "integrity": "sha1-HnvHLdWEGYLzwQTCvFeL+k+xxXI="
-        }
-    }
-}

+ 35 - 76
src/Physics/babylon.physicsHelper.ts

@@ -1,5 +1,5 @@
 module BABYLON {
 module BABYLON {
-    
+
     /**
     /**
      * The strenght of the force in correspondence to the distance of the affected object
      * The strenght of the force in correspondence to the distance of the affected object
      */
      */
@@ -7,33 +7,33 @@ module BABYLON {
         Constant, // impulse is constant in strength across it's whole radius
         Constant, // impulse is constant in strength across it's whole radius
         Linear // impulse gets weaker if it's further from the origin
         Linear // impulse gets weaker if it's further from the origin
     }
     }
-    
+
     export class PhysicsHelper {
     export class PhysicsHelper {
-        
+
         private _scene: Scene;
         private _scene: Scene;
         private _physicsEngine: Nullable<PhysicsEngine>;
         private _physicsEngine: Nullable<PhysicsEngine>;
 
 
         constructor(scene: Scene) {
         constructor(scene: Scene) {
             this._scene = scene;
             this._scene = scene;
             this._physicsEngine = this._scene.getPhysicsEngine();
             this._physicsEngine = this._scene.getPhysicsEngine();
-            
+
             if (!this._physicsEngine) {
             if (!this._physicsEngine) {
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you can use the methods.');
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you can use the methods.');
             }
             }
         }
         }
-        
+
         /**
         /**
          * @param {Vector3} origin the origin of the explosion
          * @param {Vector3} origin the origin of the explosion
          * @param {number} radius the explosion radius
          * @param {number} radius the explosion radius
          * @param {number} strength the explosion strength
          * @param {number} strength the explosion strength
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          */
          */
-        public applyRadialExplosionImpulse(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
+        public applyRadialExplosionImpulse(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant): Nullable<PhysicsRadialExplosionEvent> {
             if (!this._physicsEngine) {
             if (!this._physicsEngine) {
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call this method.');
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call this method.');
                 return null;
                 return null;
             }
             }
-            
+
             var impostors = this._physicsEngine.getImpostors();
             var impostors = this._physicsEngine.getImpostors();
             if (impostors.length === 0) {
             if (impostors.length === 0) {
                 return null;
                 return null;
@@ -41,24 +41,15 @@ module BABYLON {
 
 
             var event = new PhysicsRadialExplosionEvent(this._scene);
             var event = new PhysicsRadialExplosionEvent(this._scene);
 
 
-            for (var i = 0; i < impostors.length; ++i) {
-                var impostor = impostors[i];
-                var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(
-                    impostor,
-                    origin,
-                    radius,
-                    strength,
-                    falloff
-                );
-                if (impostorForceAndContactPoint === null) {
-                    continue;
+            impostors.forEach(impostor => {
+
+                var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(impostor, origin, radius, strength, falloff);
+                if (!impostorForceAndContactPoint) {
+                    return;
                 }
                 }
 
 
-                impostor.applyImpulse(
-                    impostorForceAndContactPoint.force,
-                    impostorForceAndContactPoint.contactPoint
-                );
-            }
+                impostor.applyImpulse(impostorForceAndContactPoint.force, impostorForceAndContactPoint.contactPoint);
+            });
 
 
             event.cleanup(false);
             event.cleanup(false);
 
 
@@ -71,12 +62,12 @@ module BABYLON {
          * @param {number} strength the explosion strength
          * @param {number} strength the explosion strength
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          */
          */
-        public applyRadialExplosionForce(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
+        public applyRadialExplosionForce(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant): Nullable<PhysicsRadialExplosionEvent> {
             if (!this._physicsEngine) {
             if (!this._physicsEngine) {
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
                 return null;
                 return null;
             }
             }
-            
+
             var impostors = this._physicsEngine.getImpostors();
             var impostors = this._physicsEngine.getImpostors();
             if (impostors.length === 0) {
             if (impostors.length === 0) {
                 return null;
                 return null;
@@ -84,24 +75,15 @@ module BABYLON {
 
 
             var event = new PhysicsRadialExplosionEvent(this._scene);
             var event = new PhysicsRadialExplosionEvent(this._scene);
 
 
-            for (var i = 0; i < impostors.length; ++i) {
-                var impostor = impostors[i];
-                var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(
-                    impostor,
-                    origin,
-                    radius,
-                    strength,
-                    falloff
-                );
-                if (impostorForceAndContactPoint === null) {
-                    continue;
+            impostors.forEach(impostor => {
+                var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(impostor, origin, radius, strength, falloff);
+
+                if (!impostorForceAndContactPoint) {
+                    return;
                 }
                 }
 
 
-                impostor.applyForce(
-                    impostorForceAndContactPoint.force,
-                    impostorForceAndContactPoint.contactPoint
-                );
-            }
+                impostor.applyForce(impostorForceAndContactPoint.force, impostorForceAndContactPoint.contactPoint);
+            })
 
 
             event.cleanup(false);
             event.cleanup(false);
 
 
@@ -114,7 +96,7 @@ module BABYLON {
          * @param {number} strength the explosion strength
          * @param {number} strength the explosion strength
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
          */
          */
-        public gravitationalField(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
+        public gravitationalField(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant): Nullable<PhysicsGravitationalFieldEvent> {
             if (!this._physicsEngine) {
             if (!this._physicsEngine) {
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
                 Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
                 return null;
                 return null;
@@ -125,14 +107,7 @@ module BABYLON {
                 return null;
                 return null;
             }
             }
 
 
-            var event = new PhysicsGravitationalFieldEvent(
-                this,
-                this._scene,
-                origin,
-                radius,
-                strength,
-                falloff
-            );
+            var event = new PhysicsGravitationalFieldEvent(this, this._scene, origin, radius, strength, falloff);
 
 
             event.cleanup(false);
             event.cleanup(false);
 
 
@@ -143,7 +118,7 @@ module BABYLON {
     /***** Radial explosion *****/
     /***** Radial explosion *****/
 
 
     export class PhysicsRadialExplosionEvent {
     export class PhysicsRadialExplosionEvent {
-        
+
         private _scene: Scene;
         private _scene: Scene;
         private _radialSphere: Mesh; // create a sphere, so we can get the intersecting meshes inside
         private _radialSphere: Mesh; // create a sphere, so we can get the intersecting meshes inside
         private _rays: Array<Ray> = [];
         private _rays: Array<Ray> = [];
@@ -197,7 +172,7 @@ module BABYLON {
                 return null;
                 return null;
             }
             }
 
 
-            var distanceFromOrigin = BABYLON.Vector3.Distance(origin, contactPoint);
+            var distanceFromOrigin = Vector3.Distance(origin, contactPoint);
             if (distanceFromOrigin > radius) {
             if (distanceFromOrigin > radius) {
                 return null;
                 return null;
             }
             }
@@ -229,14 +204,9 @@ module BABYLON {
 
 
         /*** Helpers ***/
         /*** Helpers ***/
 
 
-        private _prepareRadialSphere() {
+        private _prepareRadialSphere(): void {
             if (!this._radialSphere) {
             if (!this._radialSphere) {
-                this._radialSphere = BABYLON.Mesh.CreateSphere(
-                    "radialSphere",
-                    32,
-                    1,
-                    this._scene
-                );
+                this._radialSphere = MeshBuilder.CreateSphere("radialSphere", { segments: 32, diameter: 1 }, this._scene);
                 this._radialSphere.isVisible = false;
                 this._radialSphere.isVisible = false;
             }
             }
         }
         }
@@ -251,10 +221,7 @@ module BABYLON {
             this._radialSphere._updateBoundingInfo();
             this._radialSphere._updateBoundingInfo();
             this._radialSphere.computeWorldMatrix(true);
             this._radialSphere.computeWorldMatrix(true);
 
 
-            return this._radialSphere.intersectsMesh(
-                impostorObject,
-                true
-            );
+            return this._radialSphere.intersectsMesh(impostorObject, true);
         }
         }
 
 
     }
     }
@@ -347,20 +314,12 @@ module BABYLON {
         private _tick() {
         private _tick() {
             // Since the params won't change, we fetch the event only once
             // Since the params won't change, we fetch the event only once
             if (this._radialSphere) {
             if (this._radialSphere) {
-                this._physicsHelper.applyRadialExplosionForce(
-                    this._origin,
-                    this._radius,
-                    this._strength * -1,
-                    this._falloff
-                );
+                this._physicsHelper.applyRadialExplosionForce(this._origin, this._radius, this._strength * -1, this._falloff);
             } else {
             } else {
-                var radialExplosionEvent = <PhysicsRadialExplosionEvent>this._physicsHelper.applyRadialExplosionForce(
-                    this._origin,
-                    this._radius,
-                    this._strength * -1,
-                    this._falloff
-                );
-                this._radialSphere = <Mesh>radialExplosionEvent.getData().radialSphere.clone('radialSphereClone');
+                var radialExplosionEvent = this._physicsHelper.applyRadialExplosionForce(this._origin, this._radius, this._strength * -1, this._falloff);
+                if (radialExplosionEvent) {
+                    this._radialSphere = <Mesh>radialExplosionEvent.getData().radialSphere.clone('radialSphereClone');
+                }
             }
             }
         }
         }