Преглед на файлове

World step with delta

As per Schteppe's suggestion, the world step should be fixed, with the
delta as the 2nd variable.
This will provide good physics calculation for slower computers, and
will make cannon.js a lot better :)
Raanan Weber преди 9 години
родител
ревизия
b62dff2c71
променени са 2 файла, в които са добавени 22 реда и са изтрити 5 реда
  1. 11 3
      src/Physics/Plugins/babylon.cannonJSPlugin.js
  2. 11 2
      src/Physics/Plugins/babylon.cannonJSPlugin.ts

+ 11 - 3
src/Physics/Plugins/babylon.cannonJSPlugin.js

@@ -1,10 +1,14 @@
 var BABYLON;
 (function (BABYLON) {
     var CannonJSPlugin = (function () {
-        function CannonJSPlugin() {
+        function CannonJSPlugin(_useDeltaForWorldStep) {
+            if (_useDeltaForWorldStep === void 0) { _useDeltaForWorldStep = true; }
+            this._useDeltaForWorldStep = _useDeltaForWorldStep;
             this._registeredMeshes = [];
             this._physicsMaterials = [];
-            this.name = "cannon";
+            this._fixedTimeStep = 1 / 60;
+            //private _maxSubSteps : number = 15;
+            this.name = "CannonJS";
             this.updateBodyPosition = function (mesh) {
                 for (var index = 0; index < this._registeredMeshes.length; index++) {
                     var registeredMesh = this._registeredMeshes[index];
@@ -59,7 +63,7 @@ var BABYLON;
         };
         CannonJSPlugin.prototype.runOneStep = function (delta) {
             var _this = this;
-            this._world.step(delta);
+            this._world.step(this._fixedTimeStep, this._useDeltaForWorldStep ? delta * 1000 : 0);
             this._registeredMeshes.forEach(function (registeredMesh) {
                 // Body position
                 var bodyX = registeredMesh.body.position.x, bodyY = registeredMesh.body.position.y, bodyZ = registeredMesh.body.position.z;
@@ -343,9 +347,13 @@ var BABYLON;
                 var registeredMesh = this._registeredMeshes[index];
                 if (registeredMesh.mesh === mesh1) {
                     body1 = registeredMesh.body;
+                    if (body2)
+                        break;
                 }
                 else if (registeredMesh.mesh === mesh2) {
                     body2 = registeredMesh.body;
+                    if (body1)
+                        break;
                 }
             }
             if (!body1 || !body2) {

+ 11 - 2
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -18,8 +18,14 @@
         private _registeredMeshes: Array<IRegisteredMesh> = [];
         private _physicsMaterials = [];
         private _gravity: Vector3;
+        private _fixedTimeStep: number = 1/60;
+        //private _maxSubSteps : number = 15;
 
-        public name = "cannon";
+        public name = "CannonJS";
+        
+        public constructor(private _useDeltaForWorldStep: boolean = true) {
+            
+        }
 
         public initialize(iterations: number = 10): void {
             this._world = new CANNON.World();
@@ -32,7 +38,8 @@
         }
 
         public runOneStep(delta: number): void {
-            this._world.step(delta);
+            
+            this._world.step(this._fixedTimeStep, this._useDeltaForWorldStep ? delta * 1000 : 0);
 
             this._registeredMeshes.forEach((registeredMesh) => {
 
@@ -454,8 +461,10 @@
 
                 if (registeredMesh.mesh === mesh1) {
                     body1 = registeredMesh.body;
+                    if(body2) break;
                 } else if (registeredMesh.mesh === mesh2) {
                     body2 = registeredMesh.body;
+                    if(body1) break;
                 }
             }