فهرست منبع

Fixing constant update procedure.

Also fixed the gulp file order which failed on my computer.
Raanan Weber 9 سال پیش
والد
کامیت
dd693210ab

+ 2 - 2
Tools/Gulp/gulpfile.js

@@ -128,8 +128,8 @@ gulp.task("build", ["workers", "shaders"], function () {
         gulp.src(config.core.files),
         gulp.src(config.extras.files),
         shadersStream,
-        workersStream, 
-        includeShadersStream
+        includeShadersStream,
+        workersStream
         )
         .pipe(concat(config.build.filename))
         .pipe(cleants())

+ 4 - 68
src/Math/babylon.math.ts

@@ -10,7 +10,7 @@
         private _onChangeTriggers: Array<(changedObject: ChangableMathObject) => void> = [];
 
         public registerOnChange(func: (changedObject: ChangableMathObject) => void) {
-            if(!this._onChangeTriggers) {
+            if (!this._onChangeTriggers) {
                 this._onChangeTriggers = [];
             }
             this._onChangeTriggers.push(func);
@@ -620,37 +620,10 @@
 
     export class Vector3 extends ChangableMathObject {
 
-        constructor(private _x: number, private _y: number, private _z: number) {
+        constructor(public x: number, public y: number, public z: number) {
             super();
         }
 
-        public get x() {
-            return this._x;
-        }
-
-        public set x(x: number) {
-            this._x = x;
-            this.triggerChange();
-        }
-
-        public get y() {
-            return this._y;
-        }
-
-        public set y(y: number) {
-            this._y = y;
-            this.triggerChange();
-        }
-
-        public get z() {
-            return this._z;
-        }
-
-        public set z(z: number) {
-            this._z = z;
-            this.triggerChange();
-        }
-
         public toString(): string {
             return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
         }
@@ -1576,45 +1549,8 @@
         }
     }
 
-    export class Quaternion extends ChangableMathObject {
-        constructor(private _x: number = 0, private _y: number = 0, private _z: number = 0, private _w: number = 1) {
-            super();
-        }
-        
-        public get x() {
-            return this._x;
-        }
-
-        public set x(x: number) {
-            this._x = x;
-            this.triggerChange();
-        }
-
-        public get y() {
-            return this._y;
-        }
-
-        public set y(y: number) {
-            this._y = y;
-            this.triggerChange();
-        }
-
-        public get z() {
-            return this._z;
-        }
-
-        public set z(z: number) {
-            this._z = z;
-            this.triggerChange();
-        }
-        
-        public get w() {
-            return this._w;
-        }
-
-        public set w(w: number) {
-            this._w = w;
-            this.triggerChange();
+    export class Quaternion {
+        constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
         }
 
         public toString(): string {

+ 2 - 2
src/Mesh/babylon.abstractMesh.ts

@@ -163,7 +163,7 @@
         public set position(newPosition: Vector3) {
             this._position = newPosition;
             if (this.physicImpostor) {
-                this.position.registerOnChange(this.physicImpostor.transformationUpdated);
+                //this.position.registerOnChange(this.physicImpostor.transformationUpdated);
                 this.physicImpostor.transformationUpdated();
             }
         }
@@ -202,7 +202,7 @@
         public set rotationQuaternion(quaternion: Quaternion) {
             this._rotationQuaternion = quaternion;
             if (this.physicImpostor) {
-                this.position.registerOnChange(this.physicImpostor.transformationUpdated);
+                //this.position.registerOnChange(this.physicImpostor.transformationUpdated);
                 this.physicImpostor.transformationUpdated();
             }
         }

+ 30 - 16
src/Physics/Plugins/babylon.cannonJSPlugin.js

@@ -508,6 +508,13 @@ var BABYLON;
             this.name = "CannonJSPlugin";
             this._physicsMaterials = [];
             this._fixedTimeStep = 1 / 60;
+            this._minus90X = new BABYLON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
+            this._plus90X = new BABYLON.Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
+            this._tmpPosition = BABYLON.Vector3.Zero();
+            this._tmpQuaternion = new BABYLON.Quaternion();
+            this._tmpDeltaPosition = BABYLON.Vector3.Zero();
+            this._tmpDeltaRotation = new BABYLON.Quaternion();
+            this._tmpUnityRotation = new BABYLON.Quaternion();
             if (!this.isSupported()) {
                 BABYLON.Tools.Error("CannonJS is not available. Please make sure you included the js file.");
                 return;
@@ -571,6 +578,7 @@ var BABYLON;
                 this.world.addEventListener("preStep", impostor.beforeStep);
                 this.world.addEventListener("postStep", impostor.afterStep);
                 impostor.physicsBody.addShape(shape);
+                this.world.add(impostor.physicsBody);
                 //try to keep the body moving in the right direction by taking old properties.
                 //Should be tested!
                 if (oldBody) {
@@ -757,23 +765,22 @@ var BABYLON;
             impostor.mesh.computeWorldMatrix(true);
             // The delta between the mesh position and the mesh bounding box center
             var bbox = mesh.getBoundingInfo().boundingBox;
-            var deltaPosition = mesh.position.subtract(bbox.center);
-            var deltaRotation;
+            this._tmpDeltaPosition.copyFrom(mesh.position.subtract(bbox.center));
             var quaternion = mesh.rotationQuaternion;
-            var position = mesh.getBoundingInfo().boundingBox.center.clone();
+            this._tmpPosition.copyFrom(mesh.getBoundingInfo().boundingBox.center);
             //is shape is a plane or a heightmap, it must be rotated 90 degs in the X axis.
             if (impostor.type === BABYLON.PhysicsEngine.PlaneImpostor || impostor.type === BABYLON.PhysicsEngine.HeightmapImpostor) {
                 //-90 DEG in X, precalculated
-                var tmpQ = new BABYLON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
-                quaternion = quaternion.multiply(tmpQ);
+                quaternion = quaternion.multiply(this._minus90X);
                 //Invert! (Precalculated, 90 deg in X)
-                deltaRotation = new BABYLON.Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
+                //No need to clone. this will never change.
+                impostor.setDeltaRotation(this._plus90X);
             }
             //If it is a heightfield, if should be centered.
             if (impostor.type === BABYLON.PhysicsEngine.HeightmapImpostor) {
                 //calculate the correct body position:
                 var rotationQuaternion = mesh.rotationQuaternion;
-                mesh.rotationQuaternion = new BABYLON.Quaternion();
+                mesh.rotationQuaternion = this._tmpUnityRotation;
                 mesh.computeWorldMatrix(true);
                 //get original center with no rotation
                 var center = mesh.getBoundingInfo().boundingBox.center.clone();
@@ -786,23 +793,30 @@ var BABYLON;
                 mesh.computeWorldMatrix(true);
                 //calculate the translation
                 var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
-                position.copyFromFloats(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
+                this._tmpPosition.copyFromFloats(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
                 //add it inverted to the delta 
-                deltaPosition = mesh.getBoundingInfo().boundingBox.center.subtract(center);
-                deltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
+                this._tmpDeltaPosition.copyFrom(mesh.getBoundingInfo().boundingBox.center.subtract(center));
+                this._tmpDeltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
                 mesh.setPivotMatrix(oldPivot);
                 mesh.computeWorldMatrix(true);
             }
             else if (impostor.type === BABYLON.PhysicsEngine.MeshImpostor) {
-                deltaPosition = BABYLON.Vector3.Zero();
-                position.copyFrom(mesh.position);
+                this._tmpDeltaPosition.copyFromFloats(0, 0, 0);
+                this._tmpPosition.copyFrom(mesh.position);
             }
-            impostor.setDeltaPosition(deltaPosition);
-            impostor.setDeltaRotation(deltaRotation);
+            impostor.setDeltaPosition(this._tmpDeltaPosition);
             //Now update the impostor object
-            impostor.physicsBody.position.copy(position);
+            impostor.physicsBody.position.copy(this._tmpPosition);
             impostor.physicsBody.quaternion.copy(quaternion);
         };
+        CannonJSPlugin.prototype.setTransformationFromPhysicsBody = function (impostor) {
+            impostor.mesh.position.copyFrom(impostor.physicsBody.position);
+            impostor.mesh.rotationQuaternion.copyFrom(impostor.physicsBody.quaternion);
+        };
+        CannonJSPlugin.prototype.setPhysicsBodyTransformation = function (impostor, newPosition, newRotation) {
+            impostor.physicsBody.position.copy(newPosition);
+            impostor.physicsBody.quaternion.copy(newRotation);
+        };
         CannonJSPlugin.prototype.isSupported = function () {
             return window.CANNON !== undefined;
         };
@@ -819,6 +833,6 @@ var BABYLON;
             //nothing to do, actually.
         };
         return CannonJSPlugin;
-    })();
+    }());
     BABYLON.CannonJSPlugin = CannonJSPlugin;
 })(BABYLON || (BABYLON = {}));

+ 31 - 16
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -805,6 +805,14 @@
 
             return shape;
         }
+        
+        private _minus90X = new Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
+        private _plus90X = new Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
+        private _tmpPosition: Vector3 = Vector3.Zero();
+        private _tmpQuaternion: Quaternion = new Quaternion();  
+        private _tmpDeltaPosition: Vector3 = Vector3.Zero();
+        private _tmpDeltaRotation: Quaternion = new Quaternion();   
+        private _tmpUnityRotation: Quaternion = new Quaternion();     
 
         private _updatePhysicsBodyTransformation(impostor: PhysicsImpostor) {
             var mesh = impostor.mesh;
@@ -812,18 +820,17 @@
             impostor.mesh.computeWorldMatrix(true);
             // The delta between the mesh position and the mesh bounding box center
             var bbox = mesh.getBoundingInfo().boundingBox;
-            var deltaPosition = mesh.position.subtract(bbox.center);
-            var deltaRotation;
+            this._tmpDeltaPosition.copyFrom(mesh.position.subtract(bbox.center));
 
             var quaternion = mesh.rotationQuaternion;
-            var position = mesh.getBoundingInfo().boundingBox.center.clone();
+            this._tmpPosition.copyFrom(mesh.getBoundingInfo().boundingBox.center);
             //is shape is a plane or a heightmap, it must be rotated 90 degs in the X axis.
             if (impostor.type === PhysicsEngine.PlaneImpostor || impostor.type === PhysicsEngine.HeightmapImpostor) {
                 //-90 DEG in X, precalculated
-                var tmpQ = new Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
-                quaternion = quaternion.multiply(tmpQ);
+                quaternion = quaternion.multiply(this._minus90X);
                 //Invert! (Precalculated, 90 deg in X)
-                deltaRotation = new Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
+                //No need to clone. this will never change.
+                impostor.setDeltaRotation(this._plus90X);
             }
             
             //If it is a heightfield, if should be centered.
@@ -831,7 +838,7 @@
                 
                 //calculate the correct body position:
                 var rotationQuaternion = mesh.rotationQuaternion;
-                mesh.rotationQuaternion = new Quaternion();
+                mesh.rotationQuaternion = this._tmpUnityRotation;
                 mesh.computeWorldMatrix(true);
                 
                 //get original center with no rotation
@@ -850,25 +857,33 @@
                 //calculate the translation
                 var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
 
-                position.copyFromFloats(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
+                this._tmpPosition.copyFromFloats(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
                 //add it inverted to the delta 
-                deltaPosition = mesh.getBoundingInfo().boundingBox.center.subtract(center);
-                deltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
+                this._tmpDeltaPosition.copyFrom(mesh.getBoundingInfo().boundingBox.center.subtract(center));
+                this._tmpDeltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
 
                 mesh.setPivotMatrix(oldPivot);
                 mesh.computeWorldMatrix(true);
             } else if (impostor.type === PhysicsEngine.MeshImpostor) {
-                deltaPosition = Vector3.Zero();
-                position.copyFrom(mesh.position);
+                this._tmpDeltaPosition.copyFromFloats(0,0,0);
+                this._tmpPosition.copyFrom(mesh.position);
             }
             
-            
-            impostor.setDeltaPosition(deltaPosition);
-            impostor.setDeltaRotation(deltaRotation);
+            impostor.setDeltaPosition(this._tmpDeltaPosition);
             //Now update the impostor object
-            impostor.physicsBody.position.copy(position);
+            impostor.physicsBody.position.copy(this._tmpPosition);
             impostor.physicsBody.quaternion.copy(quaternion);
         }
+        
+        public setTransformationFromPhysicsBody(impostor: PhysicsImpostor) {
+            impostor.mesh.position.copyFrom(impostor.physicsBody.position);
+            impostor.mesh.rotationQuaternion.copyFrom(impostor.physicsBody.quaternion);
+        }
+        
+        public setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition:Vector3, newRotation: Quaternion) {
+            impostor.physicsBody.position.copy(newPosition);
+            impostor.physicsBody.quaternion.copy(newRotation);
+        }
 
         public isSupported(): boolean {
             return window.CANNON !== undefined;

+ 2 - 2
src/Physics/babylon.physicsEngine.js

@@ -2,7 +2,6 @@ var BABYLON;
 (function (BABYLON) {
     var PhysicsEngine = (function () {
         function PhysicsEngine(gravity, _physicsPlugin) {
-            if (gravity === void 0) { gravity = new BABYLON.Vector3(0, -9.807, 0); }
             if (_physicsPlugin === void 0) { _physicsPlugin = new BABYLON.CannonJSPlugin(); }
             this._physicsPlugin = _physicsPlugin;
             //new methods and parameters
@@ -12,6 +11,7 @@ var BABYLON;
                 throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. "
                     + "Please make sure it is included.");
             }
+            gravity = gravity || new BABYLON.Vector3(0, -9.807, 0);
             this.setGravity(gravity);
         }
         PhysicsEngine.prototype.setGravity = function (gravity) {
@@ -100,7 +100,7 @@ var BABYLON;
         PhysicsEngine.HeightmapImpostor = BABYLON.PhysicsImpostor.HeightmapImpostor;
         PhysicsEngine.Epsilon = 0.001;
         return PhysicsEngine;
-    })();
+    }());
     BABYLON.PhysicsEngine = PhysicsEngine;
     (function (PhysicsFeature) {
         PhysicsFeature[PhysicsFeature["PIVOT_IN_JOINT"] = 0] = "PIVOT_IN_JOINT";

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

@@ -148,6 +148,8 @@
         removeJoint(joint: PhysicsImpostorJoint)
         isSupported(): boolean;
         supports(feature: PhysicsFeature): boolean;
+        setTransformationFromPhysicsBody(impostor: PhysicsImpostor);
+        setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition:Vector3, newRotation: Quaternion);
         dispose();
     }
 }

+ 14 - 11
src/Physics/babylon.physicsImpostor.js

@@ -11,7 +11,16 @@ var BABYLON;
             this._onBeforePhysicsStepCallbacks = new Array();
             this._onAfterPhysicsStepCallbacks = new Array();
             this._onPhysicsCollideCallbacks = new Array();
+            this._deltaPosition = BABYLON.Vector3.Zero();
+            this._deltaRotation = new BABYLON.Quaternion();
+            this._tmpPositionWithDelta = BABYLON.Vector3.Zero();
+            this._tmpRotationWithDelta = new BABYLON.Quaternion();
             this.beforeStep = function () {
+                _this.mesh.position.subtractToRef(_this._deltaPosition, _this._tmpPositionWithDelta);
+                //conjugate deltaRotation
+                _this._tmpRotationWithDelta.copyFrom(_this._deltaRotation);
+                _this._tmpRotationWithDelta.multiplyInPlace(_this.mesh.rotationQuaternion);
+                _this._physicsEngine.getPhysicsPlugin().setPhysicsBodyTransformation(_this, _this._tmpPositionWithDelta, _this._tmpRotationWithDelta);
                 _this._onBeforePhysicsStepCallbacks.forEach(function (func) {
                     func(_this);
                 });
@@ -20,15 +29,9 @@ var BABYLON;
                 _this._onAfterPhysicsStepCallbacks.forEach(function (func) {
                     func(_this);
                 });
-                //update the mesh's position and rotation
-                var bodyX = _this.physicsBody.position.x, bodyY = _this.physicsBody.position.y, bodyZ = _this.physicsBody.position.z;
-                _this._mesh.position.x = bodyX + _this._deltaPosition.x;
-                _this._mesh.position.y = bodyY + _this._deltaPosition.y;
-                _this._mesh.position.z = bodyZ + _this._deltaPosition.z;
-                _this._mesh.rotationQuaternion.copyFrom(_this.physicsBody.quaternion);
-                if (_this._deltaRotation) {
-                    _this._mesh.rotationQuaternion.multiplyInPlace(_this._deltaRotation);
-                }
+                _this._physicsEngine.getPhysicsPlugin().setTransformationFromPhysicsBody(_this);
+                _this.mesh.position.addInPlace(_this._deltaPosition);
+                _this.mesh.rotationQuaternion.multiplyInPlace(_this._deltaRotation);
             };
             //event object due to cannon's architecture.
             this.onCollide = function (e) {
@@ -197,10 +200,10 @@ var BABYLON;
             }
         };
         PhysicsImpostor.prototype.setDeltaPosition = function (position) {
-            this._deltaPosition = position;
+            this._deltaPosition.copyFrom(position);
         };
         PhysicsImpostor.prototype.setDeltaRotation = function (rotation) {
-            this._deltaRotation = rotation;
+            this._deltaRotation.copyFrom(rotation);
         };
         //Impostor types
         PhysicsImpostor.NoImpostor = 0;

+ 19 - 17
src/Physics/babylon.physicsImpostor.ts

@@ -19,8 +19,8 @@ module BABYLON {
         private _onAfterPhysicsStepCallbacks = new Array<(impostor: PhysicsImpostor) => void>();
         private _onPhysicsCollideCallbacks = new Array<(collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor) => void>();
 
-        private _deltaPosition: Vector3;
-        private _deltaRotation: Quaternion;
+        private _deltaPosition: Vector3 = Vector3.Zero();
+        private _deltaRotation: Quaternion = new Quaternion();
         
         //If set, this is this impostor's parent
         private _parent: PhysicsImpostor;
@@ -162,7 +162,18 @@ module BABYLON {
             }
         }
 
+        private _tmpPositionWithDelta: Vector3 = Vector3.Zero();
+        private _tmpRotationWithDelta: Quaternion = new Quaternion();
+
         public beforeStep = () => {
+            
+            this.mesh.position.subtractToRef(this._deltaPosition, this._tmpPositionWithDelta);
+            //conjugate deltaRotation
+            this._tmpRotationWithDelta.copyFrom(this._deltaRotation);
+            this._tmpRotationWithDelta.multiplyInPlace(this.mesh.rotationQuaternion);
+            
+            this._physicsEngine.getPhysicsPlugin().setPhysicsBodyTransformation(this, this._tmpPositionWithDelta, this._tmpRotationWithDelta);
+            
             this._onBeforePhysicsStepCallbacks.forEach((func) => {
                 func(this);
             });
@@ -173,19 +184,10 @@ module BABYLON {
                 func(this);
             });
             
-            //update the mesh's position and rotation
-            var bodyX = this.physicsBody.position.x,
-                bodyY = this.physicsBody.position.y,
-                bodyZ = this.physicsBody.position.z;
-
-            this._mesh.position.x = bodyX + this._deltaPosition.x;
-            this._mesh.position.y = bodyY + this._deltaPosition.y;
-            this._mesh.position.z = bodyZ + this._deltaPosition.z;
-
-            this._mesh.rotationQuaternion.copyFrom(this.physicsBody.quaternion);
-            if (this._deltaRotation) {
-                this._mesh.rotationQuaternion.multiplyInPlace(this._deltaRotation);
-            }
+            this._physicsEngine.getPhysicsPlugin().setTransformationFromPhysicsBody(this);
+            
+            this.mesh.position.addInPlace(this._deltaPosition)
+            this.mesh.rotationQuaternion.multiplyInPlace(this._deltaRotation);
         }
         
         //event object due to cannon's architecture.
@@ -237,11 +239,11 @@ module BABYLON {
         }
         
         public setDeltaPosition(position: Vector3) {
-            this._deltaPosition = position;
+            this._deltaPosition.copyFrom(position);
         }
         
         public setDeltaRotation(rotation: Quaternion) {
-            this._deltaRotation = rotation;
+            this._deltaRotation.copyFrom(rotation);
         }
         
         //Impostor types