瀏覽代碼

refactoring physicsViewer

Adam Bowman 8 年之前
父節點
當前提交
f45228c378

+ 67 - 1
src/Debug/babylon.physicsViewer.ts

@@ -9,6 +9,10 @@
         protected _physicsEnginePlugin:IPhysicsEnginePlugin;
         private _renderFunction: () => void;
 
+        private _debugBoxMesh:Mesh;
+        private _debugSphereMesh:Mesh;
+        private _debugMaterial:StandardMaterial;
+
         constructor(scene:Scene){
 
             this._scene = scene || Engine.LastCreatedScene;
@@ -38,7 +42,7 @@
                 }
             }
 
-            var debugMesh = this._physicsEnginePlugin.getDebugMesh(impostor, this._scene);
+            var debugMesh = this._getDebugMesh(impostor, this._scene);
 
             if(debugMesh){
                 this._impostors[this._numMeshes] = impostor;
@@ -83,12 +87,74 @@
 
         }
 
+        private _getDebugMaterial(scene:Scene):Material{
+            if(!this._debugMaterial){
+                this._debugMaterial = new StandardMaterial('', scene);
+                this._debugMaterial.wireframe = true;
+            }
+            
+            return this._debugMaterial;
+        }
+
+        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
+            if(!this._debugBoxMesh){
+                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
+                this._debugBoxMesh.renderingGroupId = 1;
+                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
+                this._debugBoxMesh.material = this._getDebugMaterial(scene);
+                scene.removeMesh(this._debugBoxMesh);             
+            }
+
+            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
+        }
+
+        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
+            if(!this._debugSphereMesh){
+                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
+                this._debugSphereMesh.renderingGroupId = 1;
+                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
+                this._debugSphereMesh.material = this._getDebugMaterial(scene);
+                scene.removeMesh(this._debugSphereMesh);
+            }
+
+            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
+        }
+
+        private _getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
+            var body = impostor.physicsBody;
+            var shape = body.shapes[0];
+            var mesh:AbstractMesh;
+            
+            if (impostor.type == PhysicsImpostor.BoxImpostor) {
+                mesh = this._getDebugBoxMesh(scene);
+                impostor.getBoxSizeToRef(mesh.scaling);
+            } else if(impostor.type == PhysicsImpostor.SphereImpostor){	
+                mesh = this._getDebugSphereMesh(scene);  
+                var radius = impostor.getRadius();          
+                mesh.scaling.x = radius * 2;
+                mesh.scaling.y = radius * 2;
+                mesh.scaling.z = radius * 2;	
+            }
+
+            return mesh;
+        }
+
         public dispose(){
             
             for (var i = 0; i < this._numMeshes; i++){
                 this.hideImpostor(this._impostors[i]);
             }
 
+            if(this._debugBoxMesh){
+                this._debugBoxMesh.dispose();
+            }
+            if(this._debugSphereMesh){
+                this._debugSphereMesh.dispose();
+            }
+            if(this._debugMaterial){
+                this._debugMaterial.dispose();
+            }
+
             this._impostors.length = 0;
             this._scene = null;
             this._physicsEnginePlugin = null;

+ 14 - 67
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -9,11 +9,7 @@
         private _fixedTimeStep: number = 1 / 60;
         //See https://github.com/schteppe/cannon.js/blob/gh-pages/demos/collisionFilter.html
         private _currentCollisionGroup = 2;
-
-        private _debugBoxMesh:Mesh;
-        private _debugSphereMesh:Mesh;
-        private _debugMaterial:StandardMaterial;
-
+        
         public constructor(private _useDeltaForWorldStep: boolean = true, iterations: number = 10) {
             if (!this.isSupported()) {
                 Tools.Error("CannonJS is not available. Please make sure you included the js file.");
@@ -493,59 +489,6 @@
             joint.physicsJoint.motorEquation.minForce = lowerLimit === void 0 ? -upperLimit : lowerLimit;
         }
 
-        private _getDebugMaterial(scene:Scene):Material{
-            if(!this._debugMaterial){
-                this._debugMaterial = new StandardMaterial('', scene);
-                this._debugMaterial.wireframe = true;
-            }
-            
-            return this._debugMaterial;
-        }
-
-        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
-            if(!this._debugBoxMesh){
-                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                this._debugBoxMesh.renderingGroupId = 1;
-                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugBoxMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugBoxMesh);             
-            }
-
-            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
-            if(!this._debugSphereMesh){
-                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                this._debugSphereMesh.renderingGroupId = 1;
-                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugSphereMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugSphereMesh);
-            }
-
-            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        public getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
-            var body = impostor.physicsBody;
-            var shape = body.shapes[0];
-            var mesh:AbstractMesh;
-            
-            if (shape.halfExtents) {
-                mesh = this._getDebugBoxMesh(scene);
-                mesh.scaling.x = shape.halfExtents.x * 2;
-                mesh.scaling.y = shape.halfExtents.y * 2;
-                mesh.scaling.z = shape.halfExtents.z * 2;
-            } else if(shape.boundingSphereRadius){	
-                mesh = this._getDebugSphereMesh(scene);            
-                mesh.scaling.x = shape.boundingSphereRadius * 2;
-                mesh.scaling.y = shape.boundingSphereRadius * 2;
-                mesh.scaling.z = shape.boundingSphereRadius * 2;	
-            }
-
-            return mesh;
-        }
-
         public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
             var body = impostor.physicsBody;
 
@@ -559,16 +502,20 @@
             mesh.rotationQuaternion.w = body.quaternion.w;
         }
 
+        public getRadius(impostor: PhysicsImpostor):number{
+            var shape = impostor.physicsBody.shapes[0];
+            return shape.boundingSphereRadius;
+        }
+
+        public getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3):void{
+            var shape = impostor.physicsBody.shapes[0];
+            result.x = shape.halfExtents.x * 2;
+            result.y = shape.halfExtents.y * 2;
+            result.z = shape.halfExtents.z * 2;
+        }
+
         public dispose() {
-            if(this._debugBoxMesh){
-                this._debugBoxMesh.dispose();
-            }
-            if(this._debugSphereMesh){
-                this._debugSphereMesh.dispose();
-            }
-            if(this._debugMaterial){
-                this._debugMaterial.dispose();
-            }
+            
         }
     }
 }

+ 11 - 66
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -6,10 +6,6 @@ module BABYLON {
         public world: any;
         public name: string = "OimoJSPlugin";
 
-        private _debugBoxMesh:Mesh;
-        private _debugSphereMesh:Mesh;
-        private _debugMaterial:StandardMaterial;
-        
         constructor(iterations?: number) {
             this.world = new OIMO.World(1 / 60, 2, iterations, true);
             this.world.worldscale(1);
@@ -412,59 +408,6 @@ module BABYLON {
             }
         }
 
-        private _getDebugMaterial(scene:Scene):Material{
-            if(!this._debugMaterial){
-                this._debugMaterial = new StandardMaterial('', scene);
-                this._debugMaterial.wireframe = true;
-            }
-            
-            return this._debugMaterial;
-        }
-
-        private _getDebugBoxMesh(scene:Scene):AbstractMesh{
-            if(!this._debugBoxMesh){
-                this._debugBoxMesh = MeshBuilder.CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene); 
-                this._debugBoxMesh.renderingGroupId = 1;
-                this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugBoxMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugBoxMesh);             
-            }
-
-            return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        private _getDebugSphereMesh(scene:Scene):AbstractMesh{
-            if(!this._debugSphereMesh){
-                this._debugSphereMesh = MeshBuilder.CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene); 
-                this._debugSphereMesh.renderingGroupId = 1;
-                this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
-                this._debugSphereMesh.material = this._getDebugMaterial(scene);
-                scene.removeMesh(this._debugSphereMesh);
-            }
-
-            return this._debugSphereMesh.createInstance('physicsBodyBoxViewInstance');
-        }
-
-        public getDebugMesh(impostor:PhysicsImpostor, scene:Scene):AbstractMesh{
-            var body = impostor.physicsBody;
-            var shape = body.shapes;
-            var mesh:AbstractMesh;
-            
-            if (shape.halfWidth) {
-                mesh = this._getDebugBoxMesh(scene);
-                mesh.scaling.x = shape.halfWidth * 2;
-                mesh.scaling.y = shape.halfHeight * 2;
-                mesh.scaling.z = shape.halfDepth * 2;
-            } else if(shape.radius){	
-                mesh = this._getDebugSphereMesh(scene);            
-                mesh.scaling.x = shape.radius * 2;
-                mesh.scaling.y = shape.radius * 2;
-                mesh.scaling.z = shape.radius * 2;	
-            }
-
-            return mesh;
-        }
-
         public syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor){
             var body = impostor.physicsBody;
 
@@ -478,16 +421,18 @@ module BABYLON {
             mesh.rotationQuaternion.w = body.orientation.s;
         }
 
+        public getRadius(impostor: PhysicsImpostor):number{
+            return impostor.physicsBody.shapes.radius;
+        }
+
+        public getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3):void{
+            var shape = impostor.physicsBody.shapes;
+            result.x = shape.halfWidth * 2;
+            result.y = shape.halfHeight * 2;
+            result.z = shape.halfDepth * 2;
+        }
+
         public dispose() {
-            if(this._debugBoxMesh){
-                this._debugBoxMesh.dispose();
-            }
-            if(this._debugSphereMesh){
-                this._debugSphereMesh.dispose();
-            }
-            if(this._debugMaterial){
-                this._debugMaterial.dispose();
-            }
             this.world.clear();
         }
     }

+ 2 - 1
src/Physics/babylon.physicsEngine.ts

@@ -188,7 +188,8 @@
         updateDistanceJoint(joint: PhysicsJoint, maxDistance:number, minDistance?: number);
         setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number);
         setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number);
-        getDebugMesh(impostor:PhysicsImpostor, scene:Scene);
+        getRadius(impostor: PhysicsImpostor):number;
+        getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3);
         syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor);
         dispose();
     }

+ 8 - 0
src/Physics/babylon.physicsImpostor.ts

@@ -482,6 +482,14 @@ module BABYLON {
             this._deltaRotationConjugated = this._deltaRotation.conjugate();
         }
 
+        public getBoxSizeToRef(result:Vector3){
+            this._physicsEngine.getPhysicsPlugin().getBoxSizeToRef(this, result);
+        }
+
+        public getRadius():number{
+            return this._physicsEngine.getPhysicsPlugin().getRadius(this);
+        }
+
         //Impostor types
         public static NoImpostor = 0;
         public static SphereImpostor = 1;