فهرست منبع

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 10 سال پیش
والد
کامیت
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;
 var BABYLON;
 (function (BABYLON) {
 (function (BABYLON) {
     var CannonJSPlugin = (function () {
     var CannonJSPlugin = (function () {
-        function CannonJSPlugin() {
+        function CannonJSPlugin(_useDeltaForWorldStep) {
+            if (_useDeltaForWorldStep === void 0) { _useDeltaForWorldStep = true; }
+            this._useDeltaForWorldStep = _useDeltaForWorldStep;
             this._registeredMeshes = [];
             this._registeredMeshes = [];
             this._physicsMaterials = [];
             this._physicsMaterials = [];
-            this.name = "cannon";
+            this._fixedTimeStep = 1 / 60;
+            //private _maxSubSteps : number = 15;
+            this.name = "CannonJS";
             this.updateBodyPosition = function (mesh) {
             this.updateBodyPosition = function (mesh) {
                 for (var index = 0; index < this._registeredMeshes.length; index++) {
                 for (var index = 0; index < this._registeredMeshes.length; index++) {
                     var registeredMesh = this._registeredMeshes[index];
                     var registeredMesh = this._registeredMeshes[index];
@@ -59,7 +63,7 @@ var BABYLON;
         };
         };
         CannonJSPlugin.prototype.runOneStep = function (delta) {
         CannonJSPlugin.prototype.runOneStep = function (delta) {
             var _this = this;
             var _this = this;
-            this._world.step(delta);
+            this._world.step(this._fixedTimeStep, this._useDeltaForWorldStep ? delta * 1000 : 0);
             this._registeredMeshes.forEach(function (registeredMesh) {
             this._registeredMeshes.forEach(function (registeredMesh) {
                 // Body position
                 // Body position
                 var bodyX = registeredMesh.body.position.x, bodyY = registeredMesh.body.position.y, bodyZ = registeredMesh.body.position.z;
                 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];
                 var registeredMesh = this._registeredMeshes[index];
                 if (registeredMesh.mesh === mesh1) {
                 if (registeredMesh.mesh === mesh1) {
                     body1 = registeredMesh.body;
                     body1 = registeredMesh.body;
+                    if (body2)
+                        break;
                 }
                 }
                 else if (registeredMesh.mesh === mesh2) {
                 else if (registeredMesh.mesh === mesh2) {
                     body2 = registeredMesh.body;
                     body2 = registeredMesh.body;
+                    if (body1)
+                        break;
                 }
                 }
             }
             }
             if (!body1 || !body2) {
             if (!body1 || !body2) {

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

@@ -18,8 +18,14 @@
         private _registeredMeshes: Array<IRegisteredMesh> = [];
         private _registeredMeshes: Array<IRegisteredMesh> = [];
         private _physicsMaterials = [];
         private _physicsMaterials = [];
         private _gravity: Vector3;
         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 {
         public initialize(iterations: number = 10): void {
             this._world = new CANNON.World();
             this._world = new CANNON.World();
@@ -32,7 +38,8 @@
         }
         }
 
 
         public runOneStep(delta: number): void {
         public runOneStep(delta: number): void {
-            this._world.step(delta);
+            
+            this._world.step(this._fixedTimeStep, this._useDeltaForWorldStep ? delta * 1000 : 0);
 
 
             this._registeredMeshes.forEach((registeredMesh) => {
             this._registeredMeshes.forEach((registeredMesh) => {
 
 
@@ -454,8 +461,10 @@
 
 
                 if (registeredMesh.mesh === mesh1) {
                 if (registeredMesh.mesh === mesh1) {
                     body1 = registeredMesh.body;
                     body1 = registeredMesh.body;
+                    if(body2) break;
                 } else if (registeredMesh.mesh === mesh2) {
                 } else if (registeredMesh.mesh === mesh2) {
                     body2 = registeredMesh.body;
                     body2 = registeredMesh.body;
+                    if(body1) break;
                 }
                 }
             }
             }